home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlutil.zip / REGEX.C < prev    next >
C/C++ Source or Header  |  1990-06-21  |  86KB  |  2,748 lines

  1. /* Extended regular expression matching and search library.
  2.    Copyright (C) 1985, 1989-90 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18.  
  19. /* To test, compile with -Dtest.  This Dtestable feature turns this into
  20.    a self-contained program which reads a pattern, describes how it
  21.    compiles, then reads a string and searches for it.
  22.    
  23.    On the other hand, if you compile with both -Dtest and -Dcanned you
  24.    can run some tests we've already thought of.  */
  25.  
  26.  
  27. #ifdef emacs
  28.  
  29. /* The `emacs' switch turns on certain special matching commands
  30.   that make sense only in emacs. */
  31.  
  32. #include "config.h"
  33. #include "lisp.h"
  34. #include "buffer.h"
  35. #include "syntax.h"
  36.  
  37. #else  /* !emacs */
  38.  
  39. #ifdef USG
  40. #ifndef BSTRING
  41. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  42. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  43. #define bzero(s,n)    memset((s),0,(n))
  44. #endif
  45. #endif
  46.  
  47. char *malloc ();
  48. char *realloc ();
  49.  
  50. /* Make alloca work the best possible way.  */
  51. #ifdef __GNUC__
  52. #define alloca __builtin_alloca
  53. #else
  54. #ifdef sparc
  55. #include <alloca.h>
  56. #else
  57. char *alloca ();
  58. #endif
  59. #endif
  60.  
  61. /* Define the syntax stuff, so we can do the \<...\> things.  */
  62.  
  63. #ifndef Sword /* must be non-zero in some of the tests below...  */
  64. #define Sword 1
  65. #endif
  66.  
  67. #define SYNTAX(c) re_syntax_table[c]
  68.  
  69.  
  70. #ifdef SYNTAX_TABLE
  71.  
  72. char *re_syntax_table;
  73.  
  74. #else /* !SYNTAX_TABLE */
  75.  
  76. static char re_syntax_table[256];
  77.  
  78.  
  79. static void
  80. init_syntax_once ()
  81. {
  82.    register int c;
  83.    static int done = 0;
  84.  
  85.    if (done)
  86.      return;
  87.  
  88.    bzero (re_syntax_table, sizeof re_syntax_table);
  89.  
  90.    for (c = 'a'; c <= 'z'; c++)
  91.      re_syntax_table[c] = Sword;
  92.  
  93.    for (c = 'A'; c <= 'Z'; c++)
  94.      re_syntax_table[c] = Sword;
  95.  
  96.    for (c = '0'; c <= '9'; c++)
  97.      re_syntax_table[c] = Sword;
  98.  
  99.    done = 1;
  100. }
  101.  
  102. #endif /* SYNTAX_TABLE */
  103. #endif /* emacs */
  104.  
  105. #include <stdio.h>
  106.  
  107. /* isalpha(3) etc. are used for the character classes.  */
  108. #include <ctype.h>
  109. /* Sequents are missing isgraph.  */
  110. #ifndef isgraph
  111. #define isgraph(c) (isprint((c)) && !isspace((c)))
  112. #endif
  113.  
  114. /* A way to get a fatal error.  */
  115. #include <assert.h>
  116.  
  117. /* Get the interface, including the syntax bits.  */
  118. #include "regex.h"
  119.  
  120.  
  121. /* These are the command codes that appear in compiled regular
  122.    expressions, one per byte.  Some command codes are followed by
  123.    argument bytes.  A command code can specify any interpretation
  124.    whatsoever for its arguments.  Zero-bytes may appear in the compiled
  125.    regular expression.
  126.    
  127.    The value of `exactn' is needed in search.c (search_buffer) in emacs.
  128.    So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
  129.    `exactn' we use here must also be 1.  */
  130.  
  131. enum regexpcode
  132.   {
  133.     unused=0,
  134.     exactn=1, /* Followed by one byte giving n, then by n literal bytes.  */
  135.     begline,  /* Fail unless at beginning of line.  */
  136.     endline,  /* Fail unless at end of line.  */
  137.     jump,     /* Followed by two bytes giving relative address to jump to.  */
  138.     on_failure_jump,     /* Followed by two bytes giving relative address of 
  139.                 place to resume at in case of failure.  */
  140.     finalize_jump,     /* Throw away latest failure point and then jump to 
  141.                 address.  */
  142.     maybe_finalize_jump, /* Like jump but finalize if safe to do so.
  143.                 This is used to jump back to the beginning
  144.                 of a repeat.  If the command that follows
  145.                 this jump is clearly incompatible with the
  146.                 one at the beginning of the repeat, such that
  147.                 we can be sure that there is no use backtracking
  148.                 out of repetitions already completed,
  149.                 then we finalize.  */
  150.     dummy_failure_jump,  /* Jump, and push a dummy failure point. This 
  151.                 failure point will be thrown away if an attempt 
  152.                             is made to use it for a failure. A + construct 
  153.                             makes this before the first repeat.  Also
  154.                             use it as an intermediary kind of jump when
  155.                             compiling an or construct.  */
  156.     succeed_n,     /* Used like on_failure_jump except has to succeed n times;
  157.             then gets turned into an on_failure_jump. The relative
  158.                     address following it is useless until then.  The
  159.                     address is followed by two bytes containing n.  */
  160.     jump_n,     /* Similar to jump, but jump n times only; also the relative
  161.             address following is in turn followed by yet two more bytes
  162.                     containing n.  */
  163.     set_number_at,    /* Set the following relative location to the
  164.                subsequent number.  */
  165.     anychar,     /* Matches any (more or less) one character.  */
  166.     charset,     /* Matches any one char belonging to specified set.
  167.             First following byte is number of bitmap bytes.
  168.             Then come bytes for a bitmap saying which chars are in.
  169.             Bits in each byte are ordered low-bit-first.
  170.             A character is in the set if its bit is 1.
  171.             A character too large to have a bit in the map
  172.             is automatically not in the set.  */
  173.     charset_not, /* Same parameters as charset, but match any character
  174.                     that is not one of those specified.  */
  175.     start_memory, /* Start remembering the text that is matched, for
  176.             storing in a memory register.  Followed by one
  177.                     byte containing the register number.  Register numbers
  178.                     must be in the range 0 through RE_NREGS.  */
  179.     stop_memory, /* Stop remembering the text that is matched
  180.             and store it in a memory register.  Followed by
  181.                     one byte containing the register number. Register
  182.                     numbers must be in the range 0 through RE_NREGS.  */
  183.     duplicate,   /* Match a duplicate of something remembered.
  184.             Followed by one byte containing the index of the memory 
  185.                     register.  */
  186.     before_dot,     /* Succeeds if before point.  */
  187.     at_dot,     /* Succeeds if at point.  */
  188.     after_dot,     /* Succeeds if after point.  */
  189.     begbuf,      /* Succeeds if at beginning of buffer.  */
  190.     endbuf,      /* Succeeds if at end of buffer.  */
  191.     wordchar,    /* Matches any word-constituent character.  */
  192.     notwordchar, /* Matches any char that is not a word-constituent.  */
  193.     wordbeg,     /* Succeeds if at word beginning.  */
  194.     wordend,     /* Succeeds if at word end.  */
  195.     wordbound,   /* Succeeds if at a word boundary.  */
  196.     notwordbound,/* Succeeds if not at a word boundary.  */
  197.     syntaxspec,  /* Matches any character whose syntax is specified.
  198.             followed by a byte which contains a syntax code,
  199.                     e.g., Sword.  */
  200.     notsyntaxspec /* Matches any character whose syntax differs from
  201.                      that specified.  */
  202.   };
  203.  
  204.  
  205. /* Number of failure points to allocate space for initially,
  206.    when matching.  If this number is exceeded, more space is allocated,
  207.    so it is not a hard limit.  */
  208.  
  209. #ifndef NFAILURES
  210. #define NFAILURES 80
  211. #endif
  212.  
  213. #ifndef SIGN_EXTEND_CHAR
  214. #define SIGN_EXTEND_CHAR(x) (x)
  215. #endif
  216.  
  217.  
  218. /* Store NUMBER in two contiguous bytes starting at DESTINATION.  */
  219. #define STORE_NUMBER(destination, number)                \
  220.   { (destination)[0] = (number) & 0377;                    \
  221.     (destination)[1] = (number) >> 8; }
  222.   
  223. /* Same as STORE_NUMBER, except increment the destination pointer to
  224.    the byte after where the number is stored.  Watch out that values for
  225.    DESTINATION such as p + 1 won't work, whereas p will.  */
  226. #define STORE_NUMBER_AND_INCR(destination, number)            \
  227.   { STORE_NUMBER(destination, number);                    \
  228.     (destination) += 2; }
  229.  
  230.  
  231. /* Put into DESTINATION a number stored in two contingous bytes starting
  232.    at SOURCE.  */
  233. #define EXTRACT_NUMBER(destination, source)                \
  234.   { (destination) = *(source) & 0377;                    \
  235.     (destination) += SIGN_EXTEND_CHAR (*(char *)((source) + 1)) << 8; }
  236.  
  237. /* Same as EXTRACT_NUMBER, except increment the pointer for source to
  238.    point to second byte of SOURCE.  Note that SOURCE has to be a value
  239.    such as p, not, e.g., p + 1. */
  240. #define EXTRACT_NUMBER_AND_INCR(destination, source)            \
  241.   { EXTRACT_NUMBER (destination, source);                \
  242.     (source) += 2; }
  243.  
  244.  
  245. /* Specify the precise syntax of regexps for compilation.  This provides
  246.    for compatibility for various utilities which historically have
  247.    different, incompatible syntaxes.
  248.    
  249.    The argument SYNTAX is a bit-mask comprised of the various bits
  250.    defined in regex.h.  */
  251.  
  252. int
  253. re_set_syntax (syntax)
  254.   int syntax;
  255. {
  256.   int ret;
  257.  
  258.   ret = obscure_syntax;
  259.   obscure_syntax = syntax;
  260.   return ret;
  261. }
  262.  
  263. /* Set by re_set_syntax to the current regexp syntax to recognize.  */
  264. int obscure_syntax = 0;
  265.  
  266.  
  267.  
  268. /* Macros for re_compile_pattern, which is found below these definitions.  */
  269.  
  270. #define CHAR_CLASS_MAX_LENGTH  6
  271.  
  272. /* Fetch the next character in the uncompiled pattern, translating it if
  273.    necessary.  */
  274. #define PATFETCH(c)                            \
  275.   {if (p == pend) goto end_of_pattern;                    \
  276.   c = * (unsigned char *) p++;                        \
  277.   if (translate) c = translate[c]; }
  278.  
  279. /* Fetch the next character in the uncompiled pattern, with no
  280.    translation.  */
  281. #define PATFETCH_RAW(c)                            \
  282.  {if (p == pend) goto end_of_pattern;                    \
  283.   c = * (unsigned char *) p++; }
  284.  
  285. #define PATUNFETCH p--
  286.  
  287.  
  288. /* If the buffer isn't allocated when it comes in, use this.  */
  289. #define INIT_BUF_SIZE  28
  290.  
  291. /* Make sure we have at least N more bytes of space in buffer.  */
  292. #define GET_BUFFER_SPACE(n)                        \
  293.   {                                        \
  294.     while (b - bufp->buffer + (n) >= bufp->allocated)            \
  295.       EXTEND_BUFFER;                            \
  296.   }
  297.  
  298. /* Make sure we have one more byte of buffer space and then add CH to it.  */
  299. #define BUFPUSH(ch)                            \
  300.   {                                    \
  301.     GET_BUFFER_SPACE (1);                        \
  302.     *b++ = (char) (ch);                            \
  303.   }
  304.   
  305. /* Extend the buffer by twice its current size via reallociation and
  306.    reset the pointers that pointed into the old allocation to point to
  307.    the correct places in the new allocation.  If extending the buffer
  308.    results in it being larger than 1 << 16, then flag memory exhausted.  */
  309. #define EXTEND_BUFFER                            \
  310.   { char *old_buffer = bufp->buffer;                    \
  311.     if (bufp->allocated == (1L<<16)) goto too_big;            \
  312.     bufp->allocated *= 2;                        \
  313.     if (bufp->allocated > (1L<<16)) bufp->allocated = (1L<<16);        \
  314.     bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated);    \
  315.     if (bufp->buffer == 0)                        \
  316.       goto memory_exhausted;                        \
  317.     b = (b - old_buffer) + bufp->buffer;                \
  318.     if (fixup_jump)                            \
  319.       fixup_jump = (fixup_jump - old_buffer) + bufp->buffer;        \
  320.     if (laststart)                            \
  321.       laststart = (laststart - old_buffer) + bufp->buffer;        \
  322.     begalt = (begalt - old_buffer) + bufp->buffer;            \
  323.     if (pending_exact)                            \
  324.       pending_exact = (pending_exact - old_buffer) + bufp->buffer;    \
  325.   }
  326.  
  327. /* Set the bit for character C in a character set list.  */
  328. #define SET_LIST_BIT(c)  (b[(c) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
  329.  
  330. /* Get the next unsigned number in the uncompiled pattern.  */
  331. #define GET_UNSIGNED_NUMBER(num)                     \
  332.   { if (p != pend)                             \
  333.       {                                 \
  334.         PATFETCH (c);                             \
  335.     while (isdigit (c))                         \
  336.       {                                 \
  337.         if (num < 0)                         \
  338.            num = 0;                         \
  339.             num = num * 10 + c - '0';                     \
  340.         if (p == pend)                         \
  341.            break;                             \
  342.         PATFETCH (c);                         \
  343.       }                                 \
  344.         }                                 \
  345.   }
  346.  
  347. /* Subroutines for re_compile_pattern.  */
  348. static void store_jump (), insert_jump (), store_jump_n (),
  349.         insert_jump_n (), insert_op_2 ();
  350.  
  351.  
  352. /* re_compile_pattern takes a regular-expression string
  353.    and converts it into a buffer full of byte commands for matching.
  354.  
  355.    PATTERN   is the address of the pattern string
  356.    SIZE      is the length of it.
  357.    BUFP        is a  struct re_pattern_buffer *  which points to the info
  358.          on where to store the byte commands.
  359.          This structure contains a  char *  which points to the
  360.          actual space, which should have been obtained with malloc.
  361.          re_compile_pattern may use realloc to grow the buffer space.
  362.  
  363.    The number of bytes of commands can be found out by looking in
  364.    the `struct re_pattern_buffer' that bufp pointed to, after
  365.    re_compile_pattern returns. */
  366.  
  367. char *
  368. re_compile_pattern (pattern, size, bufp)
  369.      char *pattern;
  370.      int size;
  371.      struct re_pattern_buffer *bufp;
  372. {
  373.   register char *b = bufp->buffer;
  374.   register char *p = pattern;
  375.   char *pend = pattern + size;
  376.   register unsigned c, c1;
  377.   char *p1;
  378.   unsigned char *translate = (unsigned char *) bufp->translate;
  379.  
  380.   /* Address of the count-byte of the most recently inserted `exactn'
  381.      command.  This makes it possible to tell whether a new exact-match
  382.      character can be added to that command or requires a new `exactn'
  383.      command.  */
  384.      
  385.   char *pending_exact = 0;
  386.  
  387.   /* Address of the place where a forward-jump should go to the end of
  388.      the containing expression.  Each alternative of an `or', except the
  389.      last, ends with a forward-jump of this sort.  */
  390.  
  391.   char *fixup_jump = 0;
  392.  
  393.   /* Address of start of the most recently finished expression.
  394.      This tells postfix * where to find the start of its operand.  */
  395.  
  396.   char *laststart = 0;
  397.  
  398.   /* In processing a repeat, 1 means zero matches is allowed.  */
  399.  
  400.   char zero_times_ok;
  401.  
  402.   /* In processing a repeat, 1 means many matches is allowed.  */
  403.  
  404.   char many_times_ok;
  405.  
  406.   /* Address of beginning of regexp, or inside of last \(.  */
  407.  
  408.   char *begalt = b;
  409.  
  410.   /* In processing an interval, at least this many matches must be made.  */
  411.   int lower_bound;
  412.  
  413.   /* In processing an interval, at most this many matches can be made.  */
  414.   int upper_bound;
  415.  
  416.   /* Place in pattern (i.e., the {) to which to go back if the interval
  417.      is invalid.  */
  418.   char *beg_interval = 0;
  419.   
  420.   /* Stack of information saved by \( and restored by \).
  421.      Four stack elements are pushed by each \(:
  422.        First, the value of b.
  423.        Second, the value of fixup_jump.
  424.        Third, the value of regnum.
  425.        Fourth, the value of begalt.  */
  426.  
  427.   int stackb[40];
  428.   int *stackp = stackb;
  429.   int *stacke = stackb + 40;
  430.   int *stackt;
  431.  
  432.   /* Counts \('s as they are encountered.  Remembered for the matching \),
  433.      where it becomes the register number to put in the stop_memory
  434.      command.  */
  435.  
  436.   int regnum = 1;
  437.  
  438.   bufp->fastmap_accurate = 0;
  439.  
  440. #ifndef emacs
  441. #ifndef SYNTAX_TABLE
  442.   /* Initialize the syntax table.  */
  443.    init_syntax_once();
  444. #endif
  445. #endif
  446.  
  447.   if (bufp->allocated == 0)
  448.     {
  449.       bufp->allocated = INIT_BUF_SIZE;
  450.       if (bufp->buffer)
  451.     /* EXTEND_BUFFER loses when bufp->allocated is 0.  */
  452.     bufp->buffer = (char *) realloc (bufp->buffer, INIT_BUF_SIZE);
  453.       else
  454.     /* Caller did not allocate a buffer.  Do it for them.  */
  455.     bufp->buffer = (char *) malloc (INIT_BUF_SIZE);
  456.       if (!bufp->buffer) goto memory_exhausted;
  457.       begalt = b = bufp->buffer;
  458.     }
  459.  
  460.   while (p != pend)
  461.     {
  462.       PATFETCH (c);
  463.  
  464.       switch (c)
  465.     {
  466.     case '$':
  467.       {
  468.         char *p1 = p;
  469.         /* When testing what follows the $,
  470.            look past the \-constructs that don't consume anything.  */
  471.         if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  472.           while (p1 != pend)
  473.         {
  474.           if (*p1 == '\\' && p1 + 1 != pend
  475.               && (p1[1] == '<' || p1[1] == '>'
  476.               || p1[1] == '`' || p1[1] == '\''
  477. #ifdef emacs
  478.               || p1[1] == '='
  479. #endif
  480.               || p1[1] == 'b' || p1[1] == 'B'))
  481.             p1 += 2;
  482.           else
  483.             break;
  484.         }
  485.             if (obscure_syntax & RE_TIGHT_VBAR)
  486.           {
  487.         if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS) && p1 != pend)
  488.           goto normal_char;
  489.         /* Make operand of last vbar end before this `$'.  */
  490.         if (fixup_jump)
  491.           store_jump (fixup_jump, jump, b);
  492.         fixup_jump = 0;
  493.         BUFPUSH (endline);
  494.         break;
  495.           }
  496.         /* $ means succeed if at end of line, but only in special contexts.
  497.           If validly in the middle of a pattern, it is a normal character. */
  498.  
  499.             if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && p1 != pend)
  500.           goto invalid_pattern;
  501.         if (p1 == pend || *p1 == '\n'
  502.         || (obscure_syntax & RE_CONTEXT_INDEP_OPS)
  503.         || (obscure_syntax & RE_NO_BK_PARENS
  504.             ? *p1 == ')'
  505.             : *p1 == '\\' && p1[1] == ')')
  506.         || (obscure_syntax & RE_NO_BK_VBAR
  507.             ? *p1 == '|'
  508.             : *p1 == '\\' && p1[1] == '|'))
  509.           {
  510.         BUFPUSH (endline);
  511.         break;
  512.           }
  513.         goto normal_char;
  514.           }
  515.     case '^':
  516.       /* ^ means succeed if at beg of line, but only if no preceding 
  517.              pattern.  */
  518.              
  519.           if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && laststart)
  520.             goto invalid_pattern;
  521.           if (laststart && p - 2 >= pattern && p[-2] != '\n'
  522.            && !(obscure_syntax & RE_CONTEXT_INDEP_OPS))
  523.         goto normal_char;
  524.       if (obscure_syntax & RE_TIGHT_VBAR)
  525.         {
  526.           if (p != pattern + 1
  527.           && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  528.         goto normal_char;
  529.           BUFPUSH (begline);
  530.           begalt = b;
  531.         }
  532.       else
  533.         BUFPUSH (begline);
  534.       break;
  535.  
  536.     case '+':
  537.     case '?':
  538.       if ((obscure_syntax & RE_BK_PLUS_QM)
  539.           || (obscure_syntax & RE_LIMITED_OPS))
  540.         goto normal_char;
  541.     handle_plus:
  542.     case '*':
  543.       /* If there is no previous pattern, char not special. */
  544.       if (!laststart)
  545.             {
  546.               if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  547.                 goto invalid_pattern;
  548.               else if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  549.         goto normal_char;
  550.             }
  551.       /* If there is a sequence of repetition chars,
  552.          collapse it down to just one.  */
  553.       zero_times_ok = 0;
  554.       many_times_ok = 0;
  555.       while (1)
  556.         {
  557.           zero_times_ok |= c != '+';
  558.           many_times_ok |= c != '?';
  559.           if (p == pend)
  560.         break;
  561.           PATFETCH (c);
  562.           if (c == '*')
  563.         ;
  564.           else if (!(obscure_syntax & RE_BK_PLUS_QM)
  565.                && (c == '+' || c == '?'))
  566.         ;
  567.           else if ((obscure_syntax & RE_BK_PLUS_QM)
  568.                && c == '\\')
  569.         {
  570.           int c1;
  571.           PATFETCH (c1);
  572.           if (!(c1 == '+' || c1 == '?'))
  573.             {
  574.               PATUNFETCH;
  575.               PATUNFETCH;
  576.               break;
  577.             }
  578.           c = c1;
  579.         }
  580.           else
  581.         {
  582.           PATUNFETCH;
  583.           break;
  584.         }
  585.         }
  586.  
  587.       /* Star, etc. applied to an empty pattern is equivalent
  588.          to an empty pattern.  */
  589.       if (!laststart)  
  590.         break;
  591.  
  592.       /* Now we know whether or not zero matches is allowed
  593.          and also whether or not two or more matches is allowed.  */
  594.       if (many_times_ok)
  595.         {
  596.           /* If more than one repetition is allowed, put in at the
  597.                  end a backward relative jump from b to before the next
  598.                  jump we're going to put in below (which jumps from
  599.                  laststart to after this jump).  */
  600.               GET_BUFFER_SPACE (3);
  601.           store_jump (b, maybe_finalize_jump, laststart - 3);
  602.           b += 3;      /* Because store_jump put stuff here.  */
  603.         }
  604.           /* On failure, jump from laststart to b + 3, which will be the
  605.              end of the buffer after this jump is inserted.  */
  606.           GET_BUFFER_SPACE (3);
  607.       insert_jump (on_failure_jump, laststart, b + 3, b);
  608.       pending_exact = 0;
  609.       b += 3;
  610.       if (!zero_times_ok)
  611.         {
  612.           /* At least one repetition is required, so insert a
  613.                  dummy-failure before the initial on-failure-jump
  614.                  instruction of the loop. This effects a skip over that
  615.                  instruction the first time we hit that loop.  */
  616.               GET_BUFFER_SPACE (6);
  617.               insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  618.           b += 3;
  619.         }
  620.       break;
  621.  
  622.     case '.':
  623.       laststart = b;
  624.       BUFPUSH (anychar);
  625.       break;
  626.  
  627.         case '[':
  628.           if (p == pend)
  629.             goto invalid_pattern;
  630.       while (b - bufp->buffer
  631.          > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  632.         EXTEND_BUFFER;
  633.  
  634.       laststart = b;
  635.       if (*p == '^')
  636.         {
  637.               BUFPUSH (charset_not); 
  638.               p++;
  639.             }
  640.       else
  641.         BUFPUSH (charset);
  642.       p1 = p;
  643.  
  644.       BUFPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  645.       /* Clear the whole map */
  646.       bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  647.           
  648.       if ((obscure_syntax & RE_HAT_NOT_NEWLINE) && b[-2] == charset_not)
  649.             SET_LIST_BIT ('\n');
  650.  
  651.  
  652.       /* Read in characters and ranges, setting map bits.  */
  653.       while (1)
  654.         {
  655.           PATFETCH (c);
  656.  
  657.           /* If set, \ escapes characters when inside [...].  */
  658.           if ((obscure_syntax & RE_AWK_CLASS_HACK) && c == '\\')
  659.             {
  660.               PATFETCH(c1);
  661.                   SET_LIST_BIT (c1);
  662.               continue;
  663.             }
  664.               if (c == ']')
  665.                 {
  666.                   if (p == p1 + 1)
  667.                     {
  668.               /* If this is an empty bracket expression.  */
  669.                       if ((obscure_syntax & RE_NO_EMPTY_BRACKETS) 
  670.                           && p == pend)
  671.                         goto invalid_pattern;
  672.                     }
  673.                   else 
  674.             /* Stop if this isn't merely a ] inside a bracket
  675.                        expression, but rather the end of a bracket
  676.                        expression.  */
  677.                     break;
  678.                 }
  679.               /* Get a range.  */
  680.               if (p[0] == '-' && p[1] != ']')
  681.         {
  682.                   PATFETCH (c1);
  683.           PATFETCH (c1);
  684.                   
  685.           if ((obscure_syntax & RE_NO_EMPTY_RANGES) && c > c1)
  686.                     goto invalid_pattern;
  687.                     
  688.           if ((obscure_syntax & RE_NO_HYPHEN_RANGE_END) 
  689.                       && c1 == '-' && *p != ']')
  690.                     goto invalid_pattern;
  691.                     
  692.                   while (c <= c1)
  693.             {
  694.                       SET_LIST_BIT (c);
  695.                       c++;
  696.             }
  697.                 }
  698.           else if ((obscure_syntax & RE_CHAR_CLASSES)
  699.             &&  c == '[' && p[0] == ':')
  700.                 {
  701.           /* Longest valid character class word has six characters.  */
  702.                   char str[CHAR_CLASS_MAX_LENGTH];
  703.           PATFETCH (c);
  704.           c1 = 0;
  705.           /* If no ] at end.  */
  706.                   if (p == pend)
  707.                     goto invalid_pattern;
  708.           while (1)
  709.             {
  710.               /* Don't translate the ``character class'' characters.  */
  711.                       PATFETCH_RAW (c);
  712.               if (c == ':' || c == ']' || p == pend
  713.                           || c1 == CHAR_CLASS_MAX_LENGTH)
  714.                 break;
  715.               str[c1++] = c;
  716.             }
  717.           str[c1] = '\0';
  718.           if (p == pend     
  719.               || c == ']'    /* End of the bracket expression.  */
  720.                       || p[0] != ']'
  721.               || p + 1 == pend
  722.                       || (strcmp (str, "alpha") != 0 
  723.                           && strcmp (str, "upper") != 0
  724.               && strcmp (str, "lower") != 0 
  725.                           && strcmp (str, "digit") != 0
  726.               && strcmp (str, "alnum") != 0 
  727.                           && strcmp (str, "xdigit") != 0
  728.               && strcmp (str, "space") != 0 
  729.                           && strcmp (str, "print") != 0
  730.               && strcmp (str, "punct") != 0 
  731.                           && strcmp (str, "graph") != 0
  732.               && strcmp (str, "cntrl") != 0))
  733.             {
  734.                /* Undo the ending character, the letters, and leave 
  735.                           the leading : and [ (but set bits for them).  */
  736.                       c1++;
  737.               while (c1--)    
  738.             PATUNFETCH;
  739.               SET_LIST_BIT ('[');
  740.               SET_LIST_BIT (':');
  741.                 }
  742.                   else
  743.                     {
  744.                       /* The ] at the end of the character class.  */
  745.                       PATFETCH (c);                    
  746.                       if (c != ']')
  747.                         goto invalid_pattern;
  748.               for (c = 0; c < (1 << BYTEWIDTH); c++)
  749.             {
  750.               if ((strcmp (str, "alpha") == 0  && isalpha (c))
  751.                    || (strcmp (str, "upper") == 0  && isupper (c))
  752.                    || (strcmp (str, "lower") == 0  && islower (c))
  753.                    || (strcmp (str, "digit") == 0  && isdigit (c))
  754.                    || (strcmp (str, "alnum") == 0  && isalnum (c))
  755.                    || (strcmp (str, "xdigit") == 0  && isxdigit (c))
  756.                    || (strcmp (str, "space") == 0  && isspace (c))
  757.                    || (strcmp (str, "print") == 0  && isprint (c))
  758.                    || (strcmp (str, "punct") == 0  && ispunct (c))
  759.                    || (strcmp (str, "graph") == 0  && isgraph (c))
  760.                    || (strcmp (str, "cntrl") == 0  && iscntrl (c)))
  761.                 SET_LIST_BIT (c);
  762.             }
  763.             }
  764.                 }
  765.               else
  766.                 SET_LIST_BIT (c);
  767.         }
  768.  
  769.           /* Discard any character set/class bitmap bytes that are all
  770.              0 at the end of the map. Decrement the map-length byte too.  */
  771.           while ((int) b[-1] > 0 && b[b[-1] - 1] == 0) 
  772.             b[-1]--; 
  773.           b += b[-1];
  774.           break;
  775.  
  776.     case '(':
  777.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  778.         goto normal_char;
  779.       else
  780.         goto handle_open;
  781.  
  782.     case ')':
  783.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  784.         goto normal_char;
  785.       else
  786.         goto handle_close;
  787.  
  788.         case '\n':
  789.       if (! (obscure_syntax & RE_NEWLINE_OR))
  790.         goto normal_char;
  791.       else
  792.         goto handle_bar;
  793.  
  794.     case '|':
  795.       if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  796.               && (! laststart  ||  p == pend))
  797.         goto invalid_pattern;
  798.           else if (! (obscure_syntax & RE_NO_BK_VBAR))
  799.         goto normal_char;
  800.       else
  801.         goto handle_bar;
  802.  
  803.     case '{':
  804.            if (! ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  805.                   && (obscure_syntax & RE_INTERVALS)))
  806.              goto normal_char;
  807.            else
  808.              goto handle_interval;
  809.              
  810.         case '\\':
  811.       if (p == pend) goto invalid_pattern;
  812.       PATFETCH_RAW (c);
  813.       switch (c)
  814.         {
  815.         case '(':
  816.           if (obscure_syntax & RE_NO_BK_PARENS)
  817.         goto normal_backsl;
  818.         handle_open:
  819.           if (stackp == stacke) goto nesting_too_deep;
  820.  
  821.               /* Laststart should point to the start_memory that we are about
  822.                  to push (unless the pattern has RE_NREGS or more ('s).  */
  823.               *stackp++ = b - bufp->buffer;    
  824.           if (regnum < RE_NREGS)
  825.             {
  826.           BUFPUSH (start_memory);
  827.           BUFPUSH (regnum);
  828.             }
  829.           *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  830.           *stackp++ = regnum++;
  831.           *stackp++ = begalt - bufp->buffer;
  832.           fixup_jump = 0;
  833.           laststart = 0;
  834.           begalt = b;
  835.           break;
  836.  
  837.         case ')':
  838.           if (obscure_syntax & RE_NO_BK_PARENS)
  839.         goto normal_backsl;
  840.         handle_close:
  841.           if (stackp == stackb) goto unmatched_close;
  842.           begalt = *--stackp + bufp->buffer;
  843.           if (fixup_jump)
  844.         store_jump (fixup_jump, jump, b);
  845.           if (stackp[-1] < RE_NREGS)
  846.         {
  847.           BUFPUSH (stop_memory);
  848.           BUFPUSH (stackp[-1]);
  849.         }
  850.           stackp -= 2;
  851.               fixup_jump = *stackp ? *stackp + bufp->buffer - 1 : 0;
  852.               laststart = *--stackp + bufp->buffer;
  853.           break;
  854.  
  855.         case '|':
  856.               if ((obscure_syntax & RE_LIMITED_OPS)
  857.               || (obscure_syntax & RE_NO_BK_VBAR))
  858.         goto normal_backsl;
  859.         handle_bar:
  860.               if (obscure_syntax & RE_LIMITED_OPS)
  861.                 goto normal_char;
  862.           /* Insert before the previous alternative a jump which
  863.                  jumps to this alternative if the former fails.  */
  864.               GET_BUFFER_SPACE (6);
  865.               insert_jump (on_failure_jump, begalt, b + 6, b);
  866.           pending_exact = 0;
  867.           b += 3;
  868.           /* The alternative before the previous alternative has a
  869.                  jump after it which gets executed if it gets matched.
  870.                  Adjust that jump so it will jump to the previous
  871.                  alternative's analogous jump (put in below, which in
  872.                  turn will jump to the next (if any) alternative's such
  873.                  jump, etc.).  The last such jump jumps to the correct
  874.                  final destination.  */
  875.               if (fixup_jump)
  876.         store_jump (fixup_jump, jump, b);
  877.                 
  878.           /* Leave space for a jump after previous alternative---to be 
  879.                  filled in later.  */
  880.               fixup_jump = b;
  881.               b += 3;
  882.  
  883.               laststart = 0;
  884.           begalt = b;
  885.           break;
  886.  
  887.             case '{': 
  888.               if (! (obscure_syntax & RE_INTERVALS)
  889.           /* Let \{ be a literal.  */
  890.                   || ((obscure_syntax & RE_INTERVALS)
  891.                       && (obscure_syntax & RE_NO_BK_CURLY_BRACES))
  892.           /* If it's the string "\{".  */
  893.           || (p - 2 == pattern  &&  p == pend))
  894.                 goto normal_backsl;
  895.             handle_interval:
  896.           beg_interval = p - 1;        /* The {.  */
  897.               /* If there is no previous pattern, this isn't an interval.  */
  898.           if (!laststart)
  899.             {
  900.                   if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  901.             goto invalid_pattern;
  902.                   else
  903.                     goto normal_backsl;
  904.                 }
  905.               /* It also isn't an interval if not preceded by an re
  906.                  matching a single character or subexpression, or if
  907.                  the current type of intervals can't handle back
  908.                  references and the previous thing is a back reference.  */
  909.               if (! (*laststart == anychar
  910.              || *laststart == charset
  911.              || *laststart == charset_not
  912.              || *laststart == start_memory
  913.              || (*laststart == exactn  &&  laststart[1] == 1)
  914.              || (! (obscure_syntax & RE_NO_BK_REFS)
  915.                          && *laststart == duplicate)))
  916.                 {
  917.                   if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  918.                     goto normal_char;
  919.                     
  920.           /* Posix extended syntax is handled in previous
  921.                      statement; this is for Posix basic syntax.  */
  922.                   if (obscure_syntax & RE_INTERVALS)
  923.                     goto invalid_pattern;
  924.                     
  925.                   goto normal_backsl;
  926.         }
  927.               lower_bound = -1;            /* So can see if are set.  */
  928.           upper_bound = -1;
  929.               GET_UNSIGNED_NUMBER (lower_bound);
  930.           if (c == ',')
  931.         {
  932.           GET_UNSIGNED_NUMBER (upper_bound);
  933.           if (upper_bound < 0)
  934.             upper_bound = RE_DUP_MAX;
  935.         }
  936.           if (upper_bound < 0)
  937.         upper_bound = lower_bound;
  938.               if (! (obscure_syntax & RE_NO_BK_CURLY_BRACES)) 
  939.                 {
  940.                   if (c != '\\')
  941.                     goto invalid_pattern;
  942.                   PATFETCH (c);
  943.                 }
  944.           if (c != '}' || lower_bound < 0 || upper_bound > RE_DUP_MAX
  945.           || lower_bound > upper_bound 
  946.                   || ((obscure_syntax & RE_NO_BK_CURLY_BRACES) 
  947.               && p != pend  && *p == '{')) 
  948.             {
  949.           if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  950.                     goto unfetch_interval;
  951.                   else
  952.                     goto invalid_pattern;
  953.         }
  954.  
  955.           /* If upper_bound is zero, don't want to succeed at all; 
  956.           jump from laststart to b + 3, which will be the end of
  957.                  the buffer after this jump is inserted.  */
  958.                  
  959.                if (upper_bound == 0)
  960.                  {
  961.                    GET_BUFFER_SPACE (3);
  962.                    insert_jump (jump, laststart, b + 3, b);
  963.                    b += 3;
  964.                  }
  965.  
  966.                /* Otherwise, after lower_bound number of succeeds, jump
  967.                   to after the jump_n which will be inserted at the end
  968.                   of the buffer, and insert that jump_n.  */
  969.                else 
  970.          { /* Set to 5 if only one repetition is allowed and
  971.                   hence no jump_n is inserted at the current end of
  972.                       the buffer; then only space for the succeed_n is
  973.                       needed.  Otherwise, need space for both the
  974.                       succeed_n and the jump_n.  */
  975.                       
  976.                    unsigned slots_needed = upper_bound == 1 ? 5 : 10;
  977.                      
  978.                    GET_BUFFER_SPACE (slots_needed);
  979.                    /* Initialize the succeed_n to n, even though it will
  980.                       be set by its attendant set_number_at, because
  981.                       re_compile_fastmap will need to know it.  Jump to
  982.                       what the end of buffer will be after inserting
  983.                       this succeed_n and possibly appending a jump_n.  */
  984.                    insert_jump_n (succeed_n, laststart, b + slots_needed, 
  985.                           b, lower_bound);
  986.                    b += 5;     /* Just increment for the succeed_n here.  */
  987.  
  988.           /* More than one repetition is allowed, so put in at
  989.              the end of the buffer a backward jump from b to the
  990.                      succeed_n we put in above.  By the time we've gotten
  991.                      to this jump when matching, we'll have matched once
  992.                      already, so jump back only upper_bound - 1 times.  */
  993.  
  994.                    if (upper_bound > 1)
  995.                      {
  996.                        store_jump_n (b, jump_n, laststart, upper_bound - 1);
  997.                        b += 5;
  998.                        /* When hit this when matching, reset the
  999.                           preceding jump_n's n to upper_bound - 1.  */
  1000.                        BUFPUSH (set_number_at);
  1001.                GET_BUFFER_SPACE (2);
  1002.                        STORE_NUMBER_AND_INCR (b, -5);
  1003.                        STORE_NUMBER_AND_INCR (b, upper_bound - 1);
  1004.                      }
  1005.            /* When hit this when matching, set the succeed_n's n.  */
  1006.                    GET_BUFFER_SPACE (5);
  1007.            insert_op_2 (set_number_at, laststart, b, 5, lower_bound);
  1008.                    b += 5;
  1009.                  }
  1010.               pending_exact = 0;
  1011.           beg_interval = 0;
  1012.               break;
  1013.  
  1014.  
  1015.             unfetch_interval:
  1016.           /* If an invalid interval, match the characters as literals.  */
  1017.            if (beg_interval)
  1018.                  p = beg_interval;
  1019.              else
  1020.                  assert ("No interval beginning to which to backtrack" == 0);
  1021.  
  1022.                beg_interval = 0;
  1023.                PATFETCH (c);        /* normal_char expects char in `c'.  */
  1024.            goto normal_char;
  1025.            break;
  1026.  
  1027. #ifdef emacs
  1028.         case '=':
  1029.           BUFPUSH (at_dot);
  1030.           break;
  1031.  
  1032.         case 's':    
  1033.           laststart = b;
  1034.           BUFPUSH (syntaxspec);
  1035.           PATFETCH (c);
  1036.           BUFPUSH (syntax_spec_code[c]);
  1037.           break;
  1038.  
  1039.         case 'S':
  1040.           laststart = b;
  1041.           BUFPUSH (notsyntaxspec);
  1042.           PATFETCH (c);
  1043.           BUFPUSH (syntax_spec_code[c]);
  1044.           break;
  1045. #endif /* emacs */
  1046.  
  1047.         case 'w':
  1048.           laststart = b;
  1049.           BUFPUSH (wordchar);
  1050.           break;
  1051.  
  1052.         case 'W':
  1053.           laststart = b;
  1054.           BUFPUSH (notwordchar);
  1055.           break;
  1056.  
  1057.         case '<':
  1058.           BUFPUSH (wordbeg);
  1059.           break;
  1060.  
  1061.         case '>':
  1062.           BUFPUSH (wordend);
  1063.           break;
  1064.  
  1065.         case 'b':
  1066.           BUFPUSH (wordbound);
  1067.           break;
  1068.  
  1069.         case 'B':
  1070.           BUFPUSH (notwordbound);
  1071.           break;
  1072.  
  1073.         case '`':
  1074.           BUFPUSH (begbuf);
  1075.           break;
  1076.  
  1077.         case '\'':
  1078.           BUFPUSH (endbuf);
  1079.           break;
  1080.  
  1081.         case '1':
  1082.         case '2':
  1083.         case '3':
  1084.         case '4':
  1085.         case '5':
  1086.         case '6':
  1087.         case '7':
  1088.         case '8':
  1089.         case '9':
  1090.           if (obscure_syntax & RE_NO_BK_REFS)
  1091.                 goto normal_char;
  1092.               c1 = c - '0';
  1093.           if (c1 >= regnum)
  1094.         {
  1095.             if (obscure_syntax & RE_NO_EMPTY_BK_REF)
  1096.                     goto invalid_pattern;
  1097.                   else
  1098.                     goto normal_char;
  1099.                 }
  1100.               /* Can't back reference to a subexpression if inside of it.  */
  1101.               for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  1102.          if (*stackt == c1)
  1103.           goto normal_char;
  1104.           laststart = b;
  1105.           BUFPUSH (duplicate);
  1106.           BUFPUSH (c1);
  1107.           break;
  1108.  
  1109.         case '+':
  1110.         case '?':
  1111.           if (obscure_syntax & RE_BK_PLUS_QM)
  1112.         goto handle_plus;
  1113.           else
  1114.                 goto normal_backsl;
  1115.               break;
  1116.  
  1117.             default:
  1118.         normal_backsl:
  1119.           /* You might think it would be useful for \ to mean
  1120.          not to translate; but if we don't translate it
  1121.          it will never match anything.  */
  1122.           if (translate) c = translate[c];
  1123.           goto normal_char;
  1124.         }
  1125.       break;
  1126.  
  1127.     default:
  1128.     normal_char:        /* Expects the character in `c'.  */
  1129.       if (!pending_exact || pending_exact + *pending_exact + 1 != b
  1130.           || *pending_exact == 0177 || *p == '*' || *p == '^'
  1131.           || ((obscure_syntax & RE_BK_PLUS_QM)
  1132.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  1133.           : (*p == '+' || *p == '?'))
  1134.           || ((obscure_syntax & RE_INTERVALS) 
  1135.                   && ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  1136.               ? *p == '{'
  1137.                       : (p[0] == '\\' && p[1] == '{'))))
  1138.         {
  1139.           laststart = b;
  1140.           BUFPUSH (exactn);
  1141.           pending_exact = b;
  1142.           BUFPUSH (0);
  1143.         }
  1144.       BUFPUSH (c);
  1145.       (*pending_exact)++;
  1146.     }
  1147.     }
  1148.  
  1149.   if (fixup_jump)
  1150.     store_jump (fixup_jump, jump, b);
  1151.  
  1152.   if (stackp != stackb) goto unmatched_open;
  1153.  
  1154.   bufp->used = b - bufp->buffer;
  1155.   return 0;
  1156.  
  1157.  invalid_pattern:
  1158.   return "Invalid regular expression";
  1159.  
  1160.  unmatched_open:
  1161.   return "Unmatched \\(";
  1162.  
  1163.  unmatched_close:
  1164.   return "Unmatched \\)";
  1165.  
  1166.  end_of_pattern:
  1167.   return "Premature end of regular expression";
  1168.  
  1169.  nesting_too_deep:
  1170.   return "Nesting too deep";
  1171.  
  1172.  too_big:
  1173.   return "Regular expression too big";
  1174.  
  1175.  memory_exhausted:
  1176.   return "Memory exhausted";
  1177. }
  1178.  
  1179.  
  1180. /* Store a jump of the form <OPCODE> <relative address>.
  1181.    Store in the location FROM a jump operation to jump to relative
  1182.    address FROM - TO.  OPCODE is the opcode to store.  */
  1183.  
  1184. static void
  1185. store_jump (from, opcode, to)
  1186.      char *from, *to;
  1187.      char opcode;
  1188. {
  1189.   from[0] = opcode;
  1190.   STORE_NUMBER(from + 1, to - (from + 3));
  1191. }
  1192.  
  1193.  
  1194. /* Open up space before char FROM, and insert there a jump to TO.
  1195.    CURRENT_END gives the end of the storage not in use, so we know 
  1196.    how much data to copy up. OP is the opcode of the jump to insert.
  1197.  
  1198.    If you call this function, you must zero out pending_exact.  */
  1199.  
  1200. static void
  1201. insert_jump (op, from, to, current_end)
  1202.      char op;
  1203.      char *from, *to, *current_end;
  1204. {
  1205.   register char *pfrom = current_end;        /* Copy from here...  */
  1206.   register char *pto = current_end + 3;        /* ...to here.  */
  1207.  
  1208.   while (pfrom != from)                   
  1209.     *--pto = *--pfrom;
  1210.   store_jump (from, op, to);
  1211. }
  1212.  
  1213.  
  1214. /* Store a jump of the form <opcode> <relative address> <n> .
  1215.  
  1216.    Store in the location FROM a jump operation to jump to relative
  1217.    address FROM - TO.  OPCODE is the opcode to store, N is a number the
  1218.    jump uses, say, to decide how many times to jump.
  1219.    
  1220.    If you call this function, you must zero out pending_exact.  */
  1221.  
  1222. static void
  1223. store_jump_n (from, opcode, to, n)
  1224.      char *from, *to;
  1225.      char opcode;
  1226.      unsigned n;
  1227. {
  1228.   from[0] = opcode;
  1229.   STORE_NUMBER (from + 1, to - (from + 3));
  1230.   STORE_NUMBER (from + 3, n);
  1231. }
  1232.  
  1233.  
  1234. /* Similar to insert_jump, but handles a jump which needs an extra
  1235.    number to handle minimum and maximum cases.  Open up space at
  1236.    location FROM, and insert there a jump to TO.  CURRENT_END gives the
  1237.    end of the storage in use, so we know how much data to copy up. OP is
  1238.    the opcode of the jump to insert.
  1239.  
  1240.    If you call this function, you must zero out pending_exact.  */
  1241.  
  1242. static void
  1243. insert_jump_n (op, from, to, current_end, n)
  1244.      char op;
  1245.      char *from, *to, *current_end;
  1246.      unsigned n;
  1247. {
  1248.   register char *pfrom = current_end;        /* Copy from here...  */
  1249.   register char *pto = current_end + 5;        /* ...to here.  */
  1250.  
  1251.   while (pfrom != from)                   
  1252.     *--pto = *--pfrom;
  1253.   store_jump_n (from, op, to, n);
  1254. }
  1255.  
  1256.  
  1257. /* Open up space at location THERE, and insert operation OP followed by
  1258.    NUM_1 and NUM_2.  CURRENT_END gives the end of the storage in use, so
  1259.    we know how much data to copy up.
  1260.  
  1261.    If you call this function, you must zero out pending_exact.  */
  1262.  
  1263. static void
  1264. insert_op_2 (op, there, current_end, num_1, num_2)
  1265.      char op;
  1266.      char *there, *current_end;
  1267.      int num_1, num_2;
  1268. {
  1269.   register char *pfrom = current_end;        /* Copy from here...  */
  1270.   register char *pto = current_end + 5;        /* ...to here.  */
  1271.  
  1272.   while (pfrom != there)                   
  1273.     *--pto = *--pfrom;
  1274.   
  1275.   there[0] = op;
  1276.   STORE_NUMBER (there + 1, num_1);
  1277.   STORE_NUMBER (there + 3, num_2);
  1278. }
  1279.  
  1280.  
  1281.  
  1282. /* Given a pattern, compute a fastmap from it.  The fastmap records
  1283.    which of the (1 << BYTEWIDTH) possible characters can start a string
  1284.    that matches the pattern.  This fastmap is used by re_search to skip
  1285.    quickly over totally implausible text.
  1286.  
  1287.    The caller must supply the address of a (1 << BYTEWIDTH)-byte data 
  1288.    area as bufp->fastmap.
  1289.    The other components of bufp describe the pattern to be used.  */
  1290.  
  1291. void
  1292. re_compile_fastmap (bufp)
  1293.      struct re_pattern_buffer *bufp;
  1294. {
  1295.   unsigned char *pattern = (unsigned char *) bufp->buffer;
  1296.   int size = bufp->used;
  1297.   register char *fastmap = bufp->fastmap;
  1298.   register unsigned char *p = pattern;
  1299.   register unsigned char *pend = pattern + size;
  1300.   register int j, k;
  1301.   unsigned char *translate = (unsigned char *) bufp->translate;
  1302.  
  1303.   unsigned char *stackb[NFAILURES];
  1304.   unsigned char **stackp = stackb;
  1305.  
  1306.   unsigned is_a_succeed_n;
  1307.  
  1308.   bzero (fastmap, (1 << BYTEWIDTH));
  1309.   bufp->fastmap_accurate = 1;
  1310.   bufp->can_be_null = 0;
  1311.       
  1312.   while (p)
  1313.     {
  1314.       is_a_succeed_n = 0;
  1315.       if (p == pend)
  1316.     {
  1317.       bufp->can_be_null = 1;
  1318.       break;
  1319.     }
  1320. #ifdef SWITCH_ENUM_BUG
  1321.       switch ((int) ((enum regexpcode) *p++))
  1322. #else
  1323.       switch ((enum regexpcode) *p++)
  1324. #endif
  1325.     {
  1326.     case exactn:
  1327.       if (translate)
  1328.         fastmap[translate[p[1]]] = 1;
  1329.       else
  1330.         fastmap[p[1]] = 1;
  1331.       break;
  1332.  
  1333.         case begline:
  1334.         case before_dot:
  1335.     case at_dot:
  1336.     case after_dot:
  1337.     case begbuf:
  1338.     case endbuf:
  1339.     case wordbound:
  1340.     case notwordbound:
  1341.     case wordbeg:
  1342.     case wordend:
  1343.           continue;
  1344.  
  1345.     case endline:
  1346.       if (translate)
  1347.         fastmap[translate['\n']] = 1;
  1348.       else
  1349.         fastmap['\n'] = 1;
  1350.             
  1351.       if (bufp->can_be_null != 1)
  1352.         bufp->can_be_null = 2;
  1353.       break;
  1354.  
  1355.     case jump_n:
  1356.         case finalize_jump:
  1357.     case maybe_finalize_jump:
  1358.     case jump:
  1359.     case dummy_failure_jump:
  1360.           EXTRACT_NUMBER_AND_INCR (j, p);
  1361.       p += j;    
  1362.       if (j > 0)
  1363.         continue;
  1364.           /* Jump backward reached implies we just went through
  1365.          the body of a loop and matched nothing.
  1366.          Opcode jumped to should be an on_failure_jump.
  1367.          Just treat it like an ordinary jump.
  1368.          For a * loop, it has pushed its failure point already;
  1369.          If so, discard that as redundant.  */
  1370.  
  1371.           if ((enum regexpcode) *p != on_failure_jump
  1372.           && (enum regexpcode) *p != succeed_n)
  1373.         continue;
  1374.           p++;
  1375.           EXTRACT_NUMBER_AND_INCR (j, p);
  1376.           p += j;        
  1377.           if (stackp != stackb && *stackp == p)
  1378.             stackp--;
  1379.           continue;
  1380.       
  1381.         case on_failure_jump:
  1382.     handle_on_failure_jump:
  1383.           EXTRACT_NUMBER_AND_INCR (j, p);
  1384.           *++stackp = p + j;
  1385.       if (is_a_succeed_n)
  1386.             EXTRACT_NUMBER_AND_INCR (k, p);    /* Skip the n.  */
  1387.       continue;
  1388.  
  1389.     case succeed_n:
  1390.       is_a_succeed_n = 1;
  1391.           /* Get to the number of times to succeed.  */
  1392.           p += 2;        
  1393.       /* Increment p past the n for when k != 0.  */
  1394.           EXTRACT_NUMBER_AND_INCR (k, p);
  1395.           if (k == 0)
  1396.         {
  1397.               p -= 4;
  1398.               goto handle_on_failure_jump;
  1399.             }
  1400.           continue;
  1401.           
  1402.     case set_number_at:
  1403.           p += 4;
  1404.           continue;
  1405.  
  1406.         case start_memory:
  1407.     case stop_memory:
  1408.       p++;
  1409.       continue;
  1410.  
  1411.     case duplicate:
  1412.       bufp->can_be_null = 1;
  1413.       fastmap['\n'] = 1;
  1414.     case anychar:
  1415.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1416.         if (j != '\n')
  1417.           fastmap[j] = 1;
  1418.       if (bufp->can_be_null)
  1419.         return;
  1420.       /* Don't return; check the alternative paths
  1421.          so we can set can_be_null if appropriate.  */
  1422.       break;
  1423.  
  1424.     case wordchar:
  1425.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1426.         if (SYNTAX (j) == Sword)
  1427.           fastmap[j] = 1;
  1428.       break;
  1429.  
  1430.     case notwordchar:
  1431.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1432.         if (SYNTAX (j) != Sword)
  1433.           fastmap[j] = 1;
  1434.       break;
  1435.  
  1436. #ifdef emacs
  1437.     case syntaxspec:
  1438.       k = *p++;
  1439.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1440.         if (SYNTAX (j) == (enum syntaxcode) k)
  1441.           fastmap[j] = 1;
  1442.       break;
  1443.  
  1444.     case notsyntaxspec:
  1445.       k = *p++;
  1446.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1447.         if (SYNTAX (j) != (enum syntaxcode) k)
  1448.           fastmap[j] = 1;
  1449.       break;
  1450. #endif /* not emacs */
  1451.  
  1452.     case charset:
  1453.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1454.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  1455.           {
  1456.         if (translate)
  1457.           fastmap[translate[j]] = 1;
  1458.         else
  1459.           fastmap[j] = 1;
  1460.           }
  1461.       break;
  1462.  
  1463.     case charset_not:
  1464.       /* Chars beyond end of map must be allowed */
  1465.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  1466.         if (translate)
  1467.           fastmap[translate[j]] = 1;
  1468.         else
  1469.           fastmap[j] = 1;
  1470.  
  1471.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1472.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  1473.           {
  1474.         if (translate)
  1475.           fastmap[translate[j]] = 1;
  1476.         else
  1477.           fastmap[j] = 1;
  1478.           }
  1479.       break;
  1480.     }
  1481.  
  1482.       /* Get here means we have successfully found the possible starting
  1483.          characters of one path of the pattern.  We need not follow this
  1484.          path any farther.  Instead, look at the next alternative
  1485.          remembered in the stack.  */
  1486.    if (stackp != stackb)
  1487.     p = *stackp--;
  1488.       else
  1489.     break;
  1490.     }
  1491. }
  1492.  
  1493.  
  1494.  
  1495. /* Like re_search_2, below, but only one string is specified, and
  1496.    doesn't let you say where to stop matching. */
  1497.  
  1498. int
  1499. re_search (pbufp, string, size, startpos, range, regs)
  1500.      struct re_pattern_buffer *pbufp;
  1501.      char *string;
  1502.      int size, startpos, range;
  1503.      struct re_registers *regs;
  1504. {
  1505.   return re_search_2 (pbufp, (char *) 0, 0, string, size, startpos, range, 
  1506.               regs, size);
  1507. }
  1508.  
  1509.  
  1510. /* Using the compiled pattern in PBUFP->buffer, first tries to match the
  1511.    virtual concatenation of STRING1 and STRING2, starting first at index
  1512.    STARTPOS, then at STARTPOS + 1, and so on.  RANGE is the number of
  1513.    places to try before giving up.  If RANGE is negative, it searches
  1514.    backwards, i.e., the starting positions tried are STARTPOS, STARTPOS
  1515.    - 1, etc.  It is up to the caller to make sure that RANGE is not so
  1516.    large as to take the starting position outside of the input strings.
  1517.    STRING1 and STRING2 are of SIZE1 and SIZE2, respectively.  In REGS,
  1518.    return the indices of the virtual concatenation of STRING1 and
  1519.    STRING2 that matched the entire PBUFP->buffer and its contained
  1520.    subexpressions.  Do not consider matching past the index MSTOP in the
  1521.    virtual concatenation of STRING1 and STRING2.
  1522.  
  1523.    The value returned is the position in the strings at which the match
  1524.    was found, or -1 if no match was found, or -2 if error (such as
  1525.    failure stack overflow).  */
  1526.  
  1527. int
  1528. re_search_2 (pbufp, string1, size1, string2, size2, startpos, range,
  1529.          regs, mstop)
  1530.      struct re_pattern_buffer *pbufp;
  1531.      char *string1, *string2;
  1532.      int size1, size2;
  1533.      int startpos;
  1534.      register int range;
  1535.      struct re_registers *regs;
  1536.      int mstop;
  1537. {
  1538.   register char *fastmap = pbufp->fastmap;
  1539.   register unsigned char *translate = (unsigned char *) pbufp->translate;
  1540.   int total_size = size1 + size2;
  1541.   int val;
  1542.  
  1543.   /* Check for out-of-range starting position.  */
  1544.   if (startpos < 0  ||  startpos > total_size)
  1545.     return -1;
  1546.  
  1547.   /* Update the fastmap now if not correct already.  */
  1548.   if (fastmap && !pbufp->fastmap_accurate)
  1549.     re_compile_fastmap (pbufp);
  1550.   
  1551.   /* If the search isn't to be a backwards one, don't waste time in a
  1552.      long search for a pattern that says it is anchored.  */
  1553.   if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
  1554.       && range > 0)
  1555.     {
  1556.       if (startpos > 0)
  1557.     return -1;
  1558.       else
  1559.     range = 1;
  1560.     }
  1561.  
  1562.   while (1)
  1563.     { 
  1564.       /* If a fastmap is supplied, skip quickly over characters that
  1565.          cannot possibly be the start of a match.  Note, however, that
  1566.          if the pattern can possibly match the null string, we must
  1567.          test it at each starting point so that we take the first null
  1568.          string we get.  */
  1569.  
  1570.       if (fastmap && startpos < total_size && pbufp->can_be_null != 1)
  1571.     {
  1572.       if (range > 0)    /* Searching forwards.  */
  1573.         {
  1574.           register int lim = 0;
  1575.           register unsigned char *p;
  1576.           int irange = range;
  1577.           if (startpos < size1 && startpos + range >= size1)
  1578.         lim = range - (size1 - startpos);
  1579.  
  1580.           p = ((unsigned char *)
  1581.            &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
  1582.  
  1583.               while (range > lim && !fastmap[translate 
  1584.                                              ? translate[*p++]
  1585.                                              : *p++])
  1586.             range--;
  1587.           startpos += irange - range;
  1588.         }
  1589.       else                /* Searching backwards.  */
  1590.         {
  1591.           register unsigned char c;
  1592.  
  1593.               if (string1 == 0 || startpos >= size1)
  1594.         c = string2[startpos - size1];
  1595.           else 
  1596.         c = string1[startpos];
  1597.  
  1598.               c &= 0xff;
  1599.           if (translate ? !fastmap[translate[c]] : !fastmap[c])
  1600.         goto advance;
  1601.         }
  1602.     }
  1603.  
  1604.       if (range >= 0 && startpos == total_size
  1605.       && fastmap && pbufp->can_be_null == 0)
  1606.     return -1;
  1607.  
  1608.       val = re_match_2 (pbufp, string1, size1, string2, size2, startpos,
  1609.             regs, mstop);
  1610.       if (val >= 0)
  1611.     return startpos;
  1612.       if (val == -2)
  1613.     return -2;
  1614.  
  1615. #ifdef C_ALLOCA
  1616.       alloca (0);
  1617. #endif /* C_ALLOCA */
  1618.  
  1619.     advance:
  1620.       if (!range) 
  1621.         break;
  1622.       else if (range > 0) 
  1623.         {
  1624.           range--; 
  1625.           startpos++;
  1626.         }
  1627.       else
  1628.         {
  1629.           range++; 
  1630.           startpos--;
  1631.         }
  1632.     }
  1633.   return -1;
  1634. }
  1635.  
  1636.  
  1637.  
  1638. #ifndef emacs   /* emacs never uses this.  */
  1639. int
  1640. re_match (pbufp, string, size, pos, regs)
  1641.      struct re_pattern_buffer *pbufp;
  1642.      char *string;
  1643.      int size, pos;
  1644.      struct re_registers *regs;
  1645. {
  1646.   return re_match_2 (pbufp, (char *) 0, 0, string, size, pos, regs, size); 
  1647. }
  1648. #endif /* !emacs */
  1649.  
  1650.  
  1651. /* The following are used for re_match_2, defined below:  */
  1652.  
  1653. /* Roughly the maximum number of failure points on the stack.  Would be
  1654.    exactly that if always pushed MAX_NUM_FAILURE_ITEMS each time we failed.  */
  1655.    
  1656. int re_max_failures = 2000;
  1657.  
  1658. /* Routine used by re_match_2.  */
  1659. static int bcmp_translate ();
  1660.  
  1661.  
  1662. /* Structure and accessing macros used in re_match_2:  */
  1663.  
  1664. struct register_info
  1665. {
  1666.   unsigned is_active : 1;
  1667.   unsigned matched_something : 1;
  1668. };
  1669.  
  1670. #define IS_ACTIVE(R)  ((R).is_active)
  1671. #define MATCHED_SOMETHING(R)  ((R).matched_something)
  1672.  
  1673.  
  1674. /* Macros used by re_match_2:  */
  1675.  
  1676.  
  1677. /* I.e., regstart, regend, and reg_info.  */
  1678.  
  1679. #define NUM_REG_ITEMS  3
  1680.  
  1681. /* We push at most this many things on the stack whenever we
  1682.    fail.  The `+ 2' refers to PATTERN_PLACE and STRING_PLACE, which are
  1683.    arguments to the PUSH_FAILURE_POINT macro.  */
  1684.  
  1685. #define MAX_NUM_FAILURE_ITEMS   (RE_NREGS * NUM_REG_ITEMS + 2)
  1686.  
  1687.  
  1688. /* We push this many things on the stack whenever we fail.  */
  1689.  
  1690. #define NUM_FAILURE_ITEMS  (last_used_reg * NUM_REG_ITEMS + 2)
  1691.  
  1692.  
  1693. /* This pushes most of the information about the current state we will want
  1694.    if we ever fail back to it.  */
  1695.  
  1696. #define PUSH_FAILURE_POINT(pattern_place, string_place)            \
  1697.   {                                    \
  1698.     short last_used_reg, this_reg;                    \
  1699.                                     \
  1700.     /* Find out how many registers are active or have been matched.    \
  1701.        (Aside from register zero, which is only set at the end.)  */    \
  1702.     for (last_used_reg = RE_NREGS - 1; last_used_reg > 0; last_used_reg--)\
  1703.       if (regstart[last_used_reg] != (unsigned char *) -1)        \
  1704.         break;                                \
  1705.                                     \
  1706.     if (stacke - stackp < NUM_FAILURE_ITEMS)                \
  1707.       {                                    \
  1708.     unsigned char **stackx;                        \
  1709.     if (stacke - stackb > re_max_failures * MAX_NUM_FAILURE_ITEMS)    \
  1710.       return -2;                            \
  1711.                                     \
  1712.         /* Roughly double the size of the stack.  */            \
  1713.         stackx = (unsigned char **) alloca (2 * MAX_NUM_FAILURE_ITEMS    \
  1714.                             * (stacke - stackb)        \
  1715.                                             * sizeof (unsigned char *));\
  1716.     /* Only copy what is in use.  */                \
  1717.         bcopy (stackb, stackx, (stackp - stackb) * sizeof (char *));    \
  1718.     stackp = stackx + (stackp - stackb);                \
  1719.     stackb = stackx;                        \
  1720.     stacke = stackb + 2 * MAX_NUM_FAILURE_ITEMS * (stacke - stackb);\
  1721.       }                                    \
  1722.                                     \
  1723.     /* Now push the info for each of those registers.  */        \
  1724.     for (this_reg = 1; this_reg <= last_used_reg; this_reg++)        \
  1725.       {                                    \
  1726.         *stackp++ = regstart[this_reg];                    \
  1727.         *stackp++ = regend[this_reg];                    \
  1728.         *stackp++ = (unsigned char *) ®_info[this_reg];        \
  1729.       }                                    \
  1730.                                     \
  1731.     /* Push how many registers we saved.  */                \
  1732.     *stackp++ = (unsigned char *) last_used_reg;            \
  1733.                                     \
  1734.     *stackp++ = pattern_place;                                          \
  1735.     *stackp++ = string_place;                                           \
  1736.   }
  1737.   
  1738.  
  1739. /* This pops what PUSH_FAILURE_POINT pushes.  */
  1740.  
  1741. #define POP_FAILURE_POINT()                        \
  1742.   {                                    \
  1743.     int temp;                                \
  1744.     stackp -= 2;        /* Remove failure points.  */        \
  1745.     temp = (int) *--stackp;    /* How many regs pushed.  */            \
  1746.     temp *= NUM_REG_ITEMS;    /* How much to take off the stack.  */    \
  1747.     stackp -= temp;         /* Remove the register info.  */    \
  1748.   }
  1749.  
  1750.  
  1751. #define MATCHING_IN_FIRST_STRING  (dend == end_match_1)
  1752.  
  1753. /* Is true if there is a first string and if PTR is pointing anywhere
  1754.    inside it or just past the end.  */
  1755.    
  1756. #define IS_IN_FIRST_STRING(ptr)                     \
  1757.     (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  1758.  
  1759. /* Call before fetching a character with *d.  This switches over to
  1760.    string2 if necessary.  */
  1761.  
  1762. #define PREFETCH                            \
  1763.  while (d == dend)                                \
  1764.   {                                    \
  1765.     if (dend == end_match_2) goto fail;  /* end of string2 => fail.  */    \
  1766.     d = string2;           /* end of string1 => advance to string2.  */ \
  1767.     dend = end_match_2;                            \
  1768.   }
  1769.  
  1770.  
  1771. /* Call this when have matched something; it sets `matched' flags for the
  1772.    registers corresponding to the subexpressions of which we currently
  1773.    are inside.  */
  1774. #define SET_REGS_MATCHED                         \
  1775.   { unsigned this_reg;                             \
  1776.     for (this_reg = 0; this_reg < RE_NREGS; this_reg++)         \
  1777.       {                                 \
  1778.         if (IS_ACTIVE(reg_info[this_reg]))                \
  1779.           MATCHED_SOMETHING(reg_info[this_reg]) = 1;            \
  1780.         else                                \
  1781.           MATCHED_SOMETHING(reg_info[this_reg]) = 0;            \
  1782.       }                                 \
  1783.   }
  1784.  
  1785.  
  1786. /* Match the pattern described by PBUFP against the virtual
  1787.    concatenation of STRING1 and STRING2, which are of SIZE1 and SIZE2,
  1788.    respectively.  Start the match at index POS in the virtual
  1789.    concatenation of STRING1 and STRING2.  In REGS, return the indices of
  1790.    the virtual concatenation of STRING1 and STRING2 that matched the
  1791.    entire PBUFP->buffer and its contained subexpressions.  Do not
  1792.    consider matching past the index MSTOP in the virtual concatenation
  1793.    of STRING1 and STRING2.
  1794.  
  1795.    If pbufp->fastmap is nonzero, then it had better be up to date.
  1796.  
  1797.    The reason that the data to match are specified as two components
  1798.    which are to be regarded as concatenated is so this function can be
  1799.    used directly on the contents of an Emacs buffer.
  1800.  
  1801.    -1 is returned if there is no match.  -2 is returned if there is an
  1802.    error (such as match stack overflow).  Otherwise the value is the
  1803.    length of the substring which was matched.  */
  1804.  
  1805. int
  1806. re_match_2 (pbufp, string1_arg, size1, string2_arg, size2, pos, regs, mstop)
  1807.      struct re_pattern_buffer *pbufp;
  1808.      char *string1_arg, *string2_arg;
  1809.      int size1, size2;
  1810.      int pos;
  1811.      struct re_registers *regs;
  1812.      int mstop;
  1813. {
  1814.   register unsigned char *p = (unsigned char *) pbufp->buffer;
  1815.  
  1816.   /* Pointer to beyond end of buffer.  */
  1817.   register unsigned char *pend = p + pbufp->used;
  1818.  
  1819.   unsigned char *string1 = (unsigned char *) string1_arg;
  1820.   unsigned char *string2 = (unsigned char *) string2_arg;
  1821.   unsigned char *end1;        /* Just past end of first string.  */
  1822.   unsigned char *end2;        /* Just past end of second string.  */
  1823.  
  1824.   /* Pointers into string1 and string2, just past the last characters in
  1825.      each to consider matching.  */
  1826.   unsigned char *end_match_1, *end_match_2;
  1827.  
  1828.   register unsigned char *d, *dend;
  1829.   register int mcnt;            /* Multipurpose.  */
  1830.   unsigned char *translate = (unsigned char *) pbufp->translate;
  1831.   unsigned is_a_jump_n = 0;
  1832.  
  1833.  /* Failure point stack.  Each place that can handle a failure further
  1834.     down the line pushes a failure point on this stack.  It consists of
  1835.     restart, regend, and reg_info for all registers corresponding to the
  1836.     subexpressions we're currently inside, plus the number of such
  1837.     registers, and, finally, two char *'s.  The first char * is where to
  1838.     resume scanning the pattern; the second one is where to resume
  1839.     scanning the strings.  If the latter is zero, the failure point is a
  1840.     ``dummy''; if a failure happens and the failure point is a dummy, it
  1841.     gets discarded and the next next one is tried.  */
  1842.  
  1843.   unsigned char *initial_stack[MAX_NUM_FAILURE_ITEMS * NFAILURES];
  1844.   unsigned char **stackb = initial_stack;
  1845.   unsigned char **stackp = stackb;
  1846.   unsigned char **stacke = &stackb[MAX_NUM_FAILURE_ITEMS * NFAILURES];
  1847.  
  1848.  
  1849.   /* Information on the contents of registers. These are pointers into
  1850.      the input strings; they record just what was matched (on this
  1851.      attempt) by a subexpression part of the pattern, that is, the
  1852.      regnum-th regstart pointer points to where in the pattern we began
  1853.      matching and the regnum-th regend points to right after where we
  1854.      stopped matching the regnum-th subexpression.  (The zeroth register
  1855.      keeps track of what the whole pattern matches.)  */
  1856.      
  1857.   unsigned char *regstart[RE_NREGS];
  1858.   unsigned char *regend[RE_NREGS];
  1859.  
  1860.   /* The is_active field of reg_info helps us keep track of which (possibly
  1861.      nested) subexpressions we are currently in. The matched_something
  1862.      field of reg_info[reg_num] helps us tell whether or not we have
  1863.      matched any of the pattern so far this time through the reg_num-th
  1864.      subexpression.  These two fields get reset each time through any
  1865.      loop their register is in.  */
  1866.  
  1867.   struct register_info reg_info[RE_NREGS];
  1868.  
  1869.  
  1870.   /* The following record the register info as found in the above
  1871.      variables when we find a match better than any we've seen before. 
  1872.      This happens as we backtrack through the failure points, which in
  1873.      turn happens only if we have not yet matched the entire string.  */
  1874.  
  1875.   unsigned best_regs_set = 0;
  1876.   unsigned char *best_regstart[RE_NREGS];
  1877.   unsigned char *best_regend[RE_NREGS];
  1878.  
  1879.  
  1880.   /* Initialize subexpression text positions to -1 to mark ones that no
  1881.      \( or ( and \) or ) has been seen for. Also set all registers to
  1882.      inactive and mark them as not having matched anything or ever
  1883.      failed.  */
  1884.   for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  1885.     {
  1886.       regstart[mcnt] = regend[mcnt] = (unsigned char *) -1;
  1887.       IS_ACTIVE (reg_info[mcnt]) = 0;
  1888.       MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  1889.     }
  1890.   
  1891.   if (regs)
  1892.     for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  1893.       regs->start[mcnt] = regs->end[mcnt] = -1;
  1894.  
  1895.   /* Set up pointers to ends of strings.
  1896.      Don't allow the second string to be empty unless both are empty.  */
  1897.   if (size2 == 0)
  1898.     {
  1899.       string2 = string1;
  1900.       size2 = size1;
  1901.       string1 = 0;
  1902.       size1 = 0;
  1903.     }
  1904.   end1 = string1 + size1;
  1905.   end2 = string2 + size2;
  1906.  
  1907.   /* Compute where to stop matching, within the two strings.  */
  1908.   if (mstop <= size1)
  1909.     {
  1910.       end_match_1 = string1 + mstop;
  1911.       end_match_2 = string2;
  1912.     }
  1913.   else
  1914.     {
  1915.       end_match_1 = end1;
  1916.       end_match_2 = string2 + mstop - size1;
  1917.     }
  1918.  
  1919.   /* `p' scans through the pattern as `d' scans through the data. `dend'
  1920.      is the end of the input string that `d' points within. `d' is
  1921.      advanced into the following input string whenever necessary, but
  1922.      this happens before fetching; therefore, at the beginning of the
  1923.      loop, `d' can be pointing at the end of a string, but it cannot
  1924.      equal string2.  */
  1925.  
  1926.   if (size1 != 0 && pos <= size1)
  1927.     d = string1 + pos, dend = end_match_1;
  1928.   else
  1929.     d = string2 + pos - size1, dend = end_match_2;
  1930.  
  1931.  
  1932.   /* This loops over pattern commands.  It exits by returning from the
  1933.      function if match is complete, or it drops through if match fails
  1934.      at this starting point in the input data.  */
  1935.  
  1936.   while (1)
  1937.     {
  1938.       is_a_jump_n = 0;
  1939.       /* End of pattern means we might have succeeded.  */
  1940.       if (p == pend)
  1941.     {
  1942.       /* If not end of string, try backtracking.  Otherwise done.  */
  1943.           if (d != end_match_2)
  1944.         {
  1945.               if (stackp != stackb)
  1946.                 {
  1947.                   /* More failure points to try.  */
  1948.  
  1949.                   unsigned in_same_string = 
  1950.                           IS_IN_FIRST_STRING (best_regend[0]) 
  1951.                         == MATCHING_IN_FIRST_STRING;
  1952.  
  1953.                   /* If exceeds best match so far, save it.  */
  1954.                   if (! best_regs_set
  1955.                       || (in_same_string && d > best_regend[0])
  1956.                       || (! in_same_string && ! MATCHING_IN_FIRST_STRING))
  1957.                     {
  1958.                       best_regs_set = 1;
  1959.                       best_regend[0] = d;    /* Never use regstart[0].  */
  1960.                       
  1961.                       for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  1962.                         {
  1963.                           best_regstart[mcnt] = regstart[mcnt];
  1964.                           best_regend[mcnt] = regend[mcnt];
  1965.                         }
  1966.                     }
  1967.                   goto fail;           
  1968.                 }
  1969.               /* If no failure points, don't restore garbage.  */
  1970.               else if (best_regs_set)   
  1971.                 {
  1972.           restore_best_regs:
  1973.                   /* Restore best match.  */
  1974.                   d = best_regend[0];
  1975.                   
  1976.           for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  1977.             {
  1978.               regstart[mcnt] = best_regstart[mcnt];
  1979.               regend[mcnt] = best_regend[mcnt];
  1980.             }
  1981.                 }
  1982.             }
  1983.  
  1984.       /* If caller wants register contents data back, convert it 
  1985.          to indices.  */
  1986.       if (regs)
  1987.         {
  1988.           regs->start[0] = pos;
  1989.           if (MATCHING_IN_FIRST_STRING)
  1990.         regs->end[0] = d - string1;
  1991.           else
  1992.         regs->end[0] = d - string2 + size1;
  1993.           for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  1994.         {
  1995.           if (regend[mcnt] == (unsigned char *) -1)
  1996.             {
  1997.               regs->start[mcnt] = -1;
  1998.               regs->end[mcnt] = -1;
  1999.               continue;
  2000.             }
  2001.           if (IS_IN_FIRST_STRING (regstart[mcnt]))
  2002.             regs->start[mcnt] = regstart[mcnt] - string1;
  2003.           else
  2004.             regs->start[mcnt] = regstart[mcnt] - string2 + size1;
  2005.                     
  2006.           if (IS_IN_FIRST_STRING (regend[mcnt]))
  2007.             regs->end[mcnt] = regend[mcnt] - string1;
  2008.           else
  2009.             regs->end[mcnt] = regend[mcnt] - string2 + size1;
  2010.         }
  2011.         }
  2012.       return d - pos - (MATCHING_IN_FIRST_STRING 
  2013.                 ? string1 
  2014.                 : string2 - size1);
  2015.         }
  2016.  
  2017.       /* Otherwise match next pattern command.  */
  2018. #ifdef SWITCH_ENUM_BUG
  2019.       switch ((int) ((enum regexpcode) *p++))
  2020. #else
  2021.       switch ((enum regexpcode) *p++)
  2022. #endif
  2023.     {
  2024.  
  2025.     /* \( [or `(', as appropriate] is represented by start_memory,
  2026.            \) by stop_memory.  Both of those commands are followed by
  2027.            a register number in the next byte.  The text matched
  2028.            within the \( and \) is recorded under that number.  */
  2029.     case start_memory:
  2030.           regstart[*p] = d;
  2031.           IS_ACTIVE (reg_info[*p]) = 1;
  2032.           MATCHED_SOMETHING (reg_info[*p]) = 0;
  2033.           p++;
  2034.           break;
  2035.  
  2036.     case stop_memory:
  2037.           regend[*p] = d;
  2038.           IS_ACTIVE (reg_info[*p]) = 0;
  2039.  
  2040.           /* If just failed to match something this time around with a sub-
  2041.          expression that's in a loop, try to force exit from the loop.  */
  2042.           if ((! MATCHED_SOMETHING (reg_info[*p])
  2043.            || (enum regexpcode) p[-3] == start_memory)
  2044.           && (p + 1) != pend)              
  2045.             {
  2046.           register unsigned char *p2 = p + 1;
  2047.               mcnt = 0;
  2048.               switch (*p2++)
  2049.                 {
  2050.                   case jump_n:
  2051.             is_a_jump_n = 1;
  2052.                   case finalize_jump:
  2053.           case maybe_finalize_jump:
  2054.           case jump:
  2055.           case dummy_failure_jump:
  2056.                     EXTRACT_NUMBER_AND_INCR (mcnt, p2);
  2057.             if (is_a_jump_n)
  2058.               p2 += 2;
  2059.                     break;
  2060.                 }
  2061.           p2 += mcnt;
  2062.         
  2063.               /* If the next operation is a jump backwards in the pattern
  2064.              to an on_failure_jump, exit from the loop by forcing a
  2065.                  failure after pushing on the stack the on_failure_jump's 
  2066.                  jump in the pattern, and d.  */
  2067.           if (mcnt < 0 && (enum regexpcode) *p2++ == on_failure_jump)
  2068.         {
  2069.                   EXTRACT_NUMBER_AND_INCR (mcnt, p2);
  2070.                   PUSH_FAILURE_POINT (p2 + mcnt, d);
  2071.                   goto fail;
  2072.                 }
  2073.             }
  2074.             p++;
  2075.           break;
  2076.  
  2077.     /* \<digit> has been turned into a `duplicate' command which is
  2078.            followed by the numeric value of <digit> as the register number.  */
  2079.         case duplicate:
  2080.       {
  2081.         int regno = *p++;   /* Get which register to match against */
  2082.         register unsigned char *d2, *dend2;
  2083.  
  2084.         /* Where in input to try to start matching.  */
  2085.             d2 = regstart[regno];
  2086.             
  2087.             /* Where to stop matching; if both the place to start and
  2088.                the place to stop matching are in the same string, then
  2089.                set to the place to stop, otherwise, for now have to use
  2090.                the end of the first string.  */
  2091.  
  2092.             dend2 = ((IS_IN_FIRST_STRING (regstart[regno]) 
  2093.               == IS_IN_FIRST_STRING (regend[regno]))
  2094.              ? regend[regno] : end_match_1);
  2095.         while (1)
  2096.           {
  2097.         /* If necessary, advance to next segment in register
  2098.                    contents.  */
  2099.         while (d2 == dend2)
  2100.           {
  2101.             if (dend2 == end_match_2) break;
  2102.             if (dend2 == regend[regno]) break;
  2103.             d2 = string2, dend2 = regend[regno];  /* end of string1 => advance to string2. */
  2104.           }
  2105.         /* At end of register contents => success */
  2106.         if (d2 == dend2) break;
  2107.  
  2108.         /* If necessary, advance to next segment in data.  */
  2109.         PREFETCH;
  2110.  
  2111.         /* How many characters left in this segment to match.  */
  2112.         mcnt = dend - d;
  2113.                 
  2114.         /* Want how many consecutive characters we can match in
  2115.                    one shot, so, if necessary, adjust the count.  */
  2116.                 if (mcnt > dend2 - d2)
  2117.           mcnt = dend2 - d2;
  2118.                   
  2119.         /* Compare that many; failure if mismatch, else move
  2120.                    past them.  */
  2121.         if (translate 
  2122.                     ? bcmp_translate (d, d2, mcnt, translate) 
  2123.                     : bcmp (d, d2, mcnt))
  2124.           goto fail;
  2125.         d += mcnt, d2 += mcnt;
  2126.           }
  2127.       }
  2128.       break;
  2129.  
  2130.     case anychar:
  2131.       PREFETCH;      /* Fetch a data character. */
  2132.       /* Match anything but a newline, maybe even a null.  */
  2133.       if ((translate ? translate[*d] : *d) == '\n'
  2134.               || ((obscure_syntax & RE_DOT_NOT_NULL) 
  2135.                   && (translate ? translate[*d] : *d) == '\000'))
  2136.         goto fail;
  2137.       SET_REGS_MATCHED;
  2138.           d++;
  2139.       break;
  2140.  
  2141.     case charset:
  2142.     case charset_not:
  2143.       {
  2144.         int not = 0;        /* Nonzero for charset_not.  */
  2145.         register int c;
  2146.         if (*(p - 1) == (unsigned char) charset_not)
  2147.           not = 1;
  2148.  
  2149.         PREFETCH;        /* Fetch a data character. */
  2150.  
  2151.         if (translate)
  2152.           c = translate[*d];
  2153.         else
  2154.           c = *d;
  2155.  
  2156.         if (c < *p * BYTEWIDTH
  2157.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  2158.           not = !not;
  2159.  
  2160.         p += 1 + *p;
  2161.  
  2162.         if (!not) goto fail;
  2163.         SET_REGS_MATCHED;
  2164.             d++;
  2165.         break;
  2166.       }
  2167.  
  2168.     case begline:
  2169.           if ((size1 != 0 && d == string1)
  2170.               || (size1 == 0 && size2 != 0 && d == string2)
  2171.               || (d && d[-1] == '\n')
  2172.               || (size1 == 0 && size2 == 0))
  2173.             break;
  2174.           else
  2175.             goto fail;
  2176.             
  2177.     case endline:
  2178.       if (d == end2
  2179.           || (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
  2180.         break;
  2181.       goto fail;
  2182.  
  2183.     /* `or' constructs are handled by starting each alternative with
  2184.            an on_failure_jump that points to the start of the next
  2185.            alternative. Each alternative except the last ends with a
  2186.            jump to the joining point. (Actually, each jump except for
  2187.            the last one really jumps to the following jump, because
  2188.            tensioning the jumps is a hassle.)  */
  2189.  
  2190.     /* The start of a stupid repeat has an on_failure_jump that points
  2191.        past the end of the repeat text. This makes a failure point so 
  2192.            that on failure to match a repetition, matching restarts past
  2193.            as many repetitions have been found with no way to fail and
  2194.            look for another one.  */
  2195.  
  2196.     /* A smart repeat is similar but loops back to the on_failure_jump
  2197.        so that each repetition makes another failure point.  */
  2198.  
  2199.     case on_failure_jump:
  2200.         on_failure:
  2201.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2202.           PUSH_FAILURE_POINT (p + mcnt, d);
  2203.           break;
  2204.  
  2205.     /* The end of a smart repeat has a maybe_finalize_jump back.
  2206.        Change it either to a finalize_jump or an ordinary jump.  */
  2207.     case maybe_finalize_jump:
  2208.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2209.       {
  2210.         register unsigned char *p2 = p;
  2211.         /* Compare what follows with the beginning of the repeat.
  2212.            If we can establish that there is nothing that they would
  2213.            both match, we can change to finalize_jump.  */
  2214.         while (p2 + 1 != pend
  2215.            && (*p2 == (unsigned char) stop_memory
  2216.                || *p2 == (unsigned char) start_memory))
  2217.           p2 += 2;                /* Skip over reg number.  */
  2218.         if (p2 == pend)
  2219.           p[-3] = (unsigned char) finalize_jump;
  2220.         else if (*p2 == (unsigned char) exactn
  2221.              || *p2 == (unsigned char) endline)
  2222.           {
  2223.         register int c = *p2 == (unsigned char) endline ? '\n' : p2[2];
  2224.         register unsigned char *p1 = p + mcnt;
  2225.         /* p1[0] ... p1[2] are an on_failure_jump.
  2226.            Examine what follows that.  */
  2227.         if (p1[3] == (unsigned char) exactn && p1[5] != c)
  2228.           p[-3] = (unsigned char) finalize_jump;
  2229.         else if (p1[3] == (unsigned char) charset
  2230.              || p1[3] == (unsigned char) charset_not)
  2231.           {
  2232.             int not = p1[3] == (unsigned char) charset_not;
  2233.             if (c < p1[4] * BYTEWIDTH
  2234.             && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  2235.               not = !not;
  2236.             /* `not' is 1 if c would match.  */
  2237.             /* That means it is not safe to finalize.  */
  2238.             if (!not)
  2239.               p[-3] = (unsigned char) finalize_jump;
  2240.           }
  2241.           }
  2242.       }
  2243.       p -= 2;        /* Point at relative address again.  */
  2244.       if (p[-1] != (unsigned char) finalize_jump)
  2245.         {
  2246.           p[-1] = (unsigned char) jump;    
  2247.           goto nofinalize;
  2248.         }
  2249.         /* Note fall through.  */
  2250.  
  2251.     /* The end of a stupid repeat has a finalize_jump back to the
  2252.            start, where another failure point will be made which will
  2253.            point to after all the repetitions found so far.  */
  2254.  
  2255.         /* Take off failure points put on by matching on_failure_jump 
  2256.            because didn't fail.  Also remove the register information
  2257.            put on by the on_failure_jump.  */
  2258.         case finalize_jump:
  2259.           POP_FAILURE_POINT ();
  2260.         /* Note fall through.  */
  2261.         
  2262.     /* Jump without taking off any failure points.  */
  2263.         case jump:
  2264.     nofinalize:
  2265.       EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2266.       p += mcnt;
  2267.       break;
  2268.  
  2269.         case dummy_failure_jump:
  2270.           /* Normally, the on_failure_jump pushes a failure point, which
  2271.              then gets popped at finalize_jump.  We will end up at
  2272.              finalize_jump, also, and with a pattern of, say, `a+', we
  2273.              are skipping over the on_failure_jump, so we have to push
  2274.              something meaningless for finalize_jump to pop.  */
  2275.           PUSH_FAILURE_POINT (0, 0);
  2276.           goto nofinalize;
  2277.  
  2278.  
  2279.         /* Have to succeed matching what follows at least n times.  Then
  2280.           just handle like an on_failure_jump.  */
  2281.         case succeed_n: 
  2282.           EXTRACT_NUMBER (mcnt, p + 2);
  2283.           /* Originally, this is how many times we HAVE to succeed.  */
  2284.           if (mcnt)
  2285.             {
  2286.                mcnt--;
  2287.            p += 2;
  2288.                STORE_NUMBER_AND_INCR (p, mcnt);
  2289.             }
  2290.       else if (mcnt == 0)
  2291.             {
  2292.           p[2] = unused;
  2293.               p[3] = unused;
  2294.               goto on_failure;
  2295.             }
  2296.           else
  2297.             assert ("The succeed_n's n is not set" == 0);
  2298.           break;
  2299.         
  2300.         case jump_n: 
  2301.           EXTRACT_NUMBER (mcnt, p + 2);
  2302.           /* Originally, this is how many times we CAN jump.  */
  2303.           if (mcnt)
  2304.             {
  2305.                mcnt--;
  2306.                STORE_NUMBER(p + 2, mcnt);
  2307.            goto nofinalize;         /* Do the jump without taking off
  2308.                             any failure points.  */
  2309.             }
  2310.           /* If don't have to jump any more, skip over the rest of command.  */
  2311.       else      
  2312.         p += 4;             
  2313.           break;
  2314.         
  2315.     case set_number_at:
  2316.       {
  2317.           register unsigned char *p1;
  2318.  
  2319.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2320.             p1 = p + mcnt;
  2321.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2322.         STORE_NUMBER (p1, mcnt);
  2323.             break;
  2324.           }
  2325.  
  2326.         /* Ignore these. Used to ignore the n of succeed_n's which
  2327.            currently have n == 0.  */
  2328.         case unused:
  2329.           break;
  2330.  
  2331.         case wordbound:
  2332.       if (d == string1  /* Points to first char */
  2333.           || d == end2  /* Points to end */
  2334.           || (d == end1 && size2 == 0)) /* Points to end */
  2335.         break;
  2336.       if ((SYNTAX (d[-1]) == Sword)
  2337.           != (SYNTAX (d == end1 ? *string2 : *d) == Sword))
  2338.         break;
  2339.       goto fail;
  2340.  
  2341.     case notwordbound:
  2342.       if (d == string1  /* Points to first char */
  2343.           || d == end2  /* Points to end */
  2344.           || (d == end1 && size2 == 0)) /* Points to end */
  2345.         goto fail;
  2346.       if ((SYNTAX (d[-1]) == Sword)
  2347.           != (SYNTAX (d == end1 ? *string2 : *d) == Sword))
  2348.         goto fail;
  2349.       break;
  2350.  
  2351.     case wordbeg:
  2352.       if (d == end2  /* Points to end?  */
  2353.           || (d == end1 && size2 == 0) /* Points to end?  */
  2354.               /* Next char not a letter?  */
  2355.           || SYNTAX (* (d == end1 ? string2 : d)) != Sword)
  2356.         goto fail;
  2357.       if (d == string1  /* Points to first char?  */
  2358.           || SYNTAX (d[-1]) != Sword)  /* Previous char not a letter?  */
  2359.         break;
  2360.       goto fail;
  2361.  
  2362.     case wordend:
  2363.       if (d == string1  /* Points to first char?  */
  2364.           || SYNTAX (d[-1]) != Sword)  /* Previous char not a letter? */
  2365.         goto fail;
  2366.       if (d == end2  /* Points to end?  */
  2367.           || (d == end1 && size2 == 0) /* Points to end?  */
  2368.               /* Next char not a letter?  */
  2369.           || SYNTAX (d == end1 ? *string2 : *d) != Sword)
  2370.         break;
  2371.       goto fail;
  2372.  
  2373. #ifdef emacs
  2374.     case before_dot:
  2375.       if (((d - string2 <= (unsigned) size2)
  2376.            ? d - bf_p2 : d - bf_p1)
  2377.           <= point)
  2378.         goto fail;
  2379.       break;
  2380.  
  2381.     case at_dot:
  2382.       if (((d - string2 <= (unsigned) size2)
  2383.            ? d - bf_p2 : d - bf_p1)
  2384.           == point)
  2385.         goto fail;
  2386.       break;
  2387.  
  2388.     case after_dot:
  2389.       if (((d - string2 <= (unsigned) size2)
  2390.            ? d - bf_p2 : d - bf_p1)
  2391.           >= point)
  2392.         goto fail;
  2393.       break;
  2394.  
  2395.     case wordchar:
  2396.       mcnt = (int) Sword;
  2397.       goto matchsyntax;
  2398.  
  2399.     case syntaxspec:
  2400.       mcnt = *p++;
  2401.     matchsyntax:
  2402.       PREFETCH;
  2403.       if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
  2404.       break;
  2405.       
  2406.     case notwordchar:
  2407.       mcnt = (int) Sword;
  2408.       goto matchnotsyntax;
  2409.  
  2410.     case notsyntaxspec:
  2411.       mcnt = *p++;
  2412.     matchnotsyntax:
  2413.       PREFETCH;
  2414.       if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
  2415.       break;
  2416. #else
  2417.     case wordchar:
  2418.       PREFETCH;
  2419.       if (SYNTAX (*d++) == 0) goto fail;
  2420.       break;
  2421.       
  2422.     case notwordchar:
  2423.       PREFETCH;
  2424.       if (SYNTAX (*d++) != 0) goto fail;
  2425.       break;
  2426. #endif /* !emacs */
  2427.  
  2428.     case begbuf:
  2429.           if ((size1 != 0 && d == string1)
  2430.               || (size1 == 0 && size2 != 0 && d == string2)
  2431.               || (size1 == 0 && size2 == 0))
  2432.             break;
  2433.           else
  2434.             goto fail;
  2435.  
  2436.         case endbuf:
  2437.       if (d == end2 || (d == end1 && size2 == 0))
  2438.         break;
  2439.       goto fail;
  2440.  
  2441.     case exactn:
  2442.       /* Match the next few pattern characters exactly.
  2443.          mcnt is how many characters to match.  */
  2444.       mcnt = *p++;
  2445.       /* This is written out as an if-else so we don't waste time
  2446.              testing `translate' inside the loop.  */
  2447.           if (translate)
  2448.         {
  2449.           do
  2450.         {
  2451.           PREFETCH;
  2452.           if (translate[*d++] != *p++) goto fail;
  2453.         }
  2454.           while (--mcnt);
  2455.         }
  2456.       else
  2457.         {
  2458.           do
  2459.         {
  2460.           PREFETCH;
  2461.           if (*d++ != *p++) goto fail;
  2462.         }
  2463.           while (--mcnt);
  2464.         }
  2465.       SET_REGS_MATCHED;
  2466.           break;
  2467.     }
  2468.       continue;  /* Successfully executed one pattern command; keep going.  */
  2469.  
  2470.     /* Jump here if any matching operation fails. */
  2471.     fail:
  2472.       if (stackp != stackb)
  2473.     /* A restart point is known.  Restart there and pop it. */
  2474.     {
  2475.           short last_used_reg, this_reg;
  2476.           
  2477.           /* If this failure point is from a dummy_failure_point, just
  2478.              skip it.  */
  2479.       if (!stackp[-2])
  2480.             {
  2481.               POP_FAILURE_POINT ();
  2482.               goto fail;
  2483.             }
  2484.  
  2485.           d = *--stackp;
  2486.       p = *--stackp;
  2487.           if (d >= string1 && d <= end1)
  2488.         dend = end_match_1;
  2489.           /* Restore register info.  */
  2490.           last_used_reg = (short) *--stackp;
  2491.           
  2492.           /* Make the ones that weren't saved -1 or 0 again.  */
  2493.           for (this_reg = RE_NREGS - 1; this_reg > last_used_reg; this_reg--)
  2494.             {
  2495.               regend[this_reg] = (unsigned char *) -1;
  2496.               regstart[this_reg] = (unsigned char *) -1;
  2497.               IS_ACTIVE (reg_info[this_reg]) = 0;
  2498.               MATCHED_SOMETHING (reg_info[this_reg]) = 0;
  2499.             }
  2500.           
  2501.           /* And restore the rest from the stack.  */
  2502.           for ( ; this_reg > 0; this_reg--)
  2503.             {
  2504.               reg_info[this_reg] = *(struct register_info *) *--stackp;
  2505.               regend[this_reg] = *--stackp;
  2506.               regstart[this_reg] = *--stackp;
  2507.             }
  2508.     }
  2509.       else
  2510.         break;   /* Matching at this starting point really fails.  */
  2511.     }
  2512.  
  2513.   if (best_regs_set)
  2514.     goto restore_best_regs;
  2515.   return -1;                     /* Failure to match.  */
  2516. }
  2517.  
  2518.  
  2519. static int
  2520. bcmp_translate (s1, s2, len, translate)
  2521.      unsigned char *s1, *s2;
  2522.      register int len;
  2523.      unsigned char *translate;
  2524. {
  2525.   register unsigned char *p1 = s1, *p2 = s2;
  2526.   while (len)
  2527.     {
  2528.       if (translate [*p1++] != translate [*p2++]) return 1;
  2529.       len--;
  2530.     }
  2531.   return 0;
  2532. }
  2533.  
  2534.  
  2535.  
  2536. /* Entry points compatible with 4.2 BSD regex library.  */
  2537.  
  2538. #ifndef emacs
  2539.  
  2540. static struct re_pattern_buffer re_comp_buf;
  2541.  
  2542. char *
  2543. re_comp (s)
  2544.      char *s;
  2545. {
  2546.   if (!s)
  2547.     {
  2548.       if (!re_comp_buf.buffer)
  2549.     return "No previous regular expression";
  2550.       return 0;
  2551.     }
  2552.  
  2553.   if (!re_comp_buf.buffer)
  2554.     {
  2555.       if (!(re_comp_buf.buffer = (char *) malloc (200)))
  2556.     return "Memory exhausted";
  2557.       re_comp_buf.allocated = 200;
  2558.       if (!(re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH)))
  2559.     return "Memory exhausted";
  2560.     }
  2561.   return re_compile_pattern (s, strlen (s), &re_comp_buf);
  2562. }
  2563.  
  2564. int
  2565. re_exec (s)
  2566.      char *s;
  2567. {
  2568.   int len = strlen (s);
  2569.   return 0 <= re_search (&re_comp_buf, s, len, 0, len,
  2570.              (struct re_registers *) 0);
  2571. }
  2572. #endif /* !emacs */
  2573.  
  2574.  
  2575.  
  2576. #ifdef test
  2577.  
  2578. #include <stdio.h>
  2579.  
  2580. /* Indexed by a character, gives the upper case equivalent of the
  2581.    character.  */
  2582.  
  2583. char upcase[0400] = 
  2584.   { 000, 001, 002, 003, 004, 005, 006, 007,
  2585.     010, 011, 012, 013, 014, 015, 016, 017,
  2586.     020, 021, 022, 023, 024, 025, 026, 027,
  2587.     030, 031, 032, 033, 034, 035, 036, 037,
  2588.     040, 041, 042, 043, 044, 045, 046, 047,
  2589.     050, 051, 052, 053, 054, 055, 056, 057,
  2590.     060, 061, 062, 063, 064, 065, 066, 067,
  2591.     070, 071, 072, 073, 074, 075, 076, 077,
  2592.     0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  2593.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  2594.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  2595.     0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  2596.     0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  2597.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  2598.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  2599.     0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  2600.     0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  2601.     0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  2602.     0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  2603.     0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  2604.     0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  2605.     0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  2606.     0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  2607.     0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  2608.     0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  2609.     0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  2610.     0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  2611.     0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  2612.     0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  2613.     0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  2614.     0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  2615.     0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  2616.   };
  2617.  
  2618. #ifdef canned
  2619.  
  2620. #include "tests.h"
  2621.  
  2622. typedef enum { extended_test, basic_test } test_type;
  2623.  
  2624. /* Use this to run the tests we've thought of.  */
  2625.  
  2626. void
  2627. main ()
  2628. {
  2629.   test_type t = basic_test;
  2630.  
  2631.   if (t == basic_test)
  2632.     {
  2633.       printf ("Running basic tests:\n\n");
  2634.       test_posix_basic ();
  2635.     }
  2636.   else if (t == extended_test)
  2637.     {
  2638.       printf ("Running extended tests:\n\n");
  2639.       test_posix_extended (); 
  2640.     }
  2641. }
  2642.  
  2643. #else /* !canned */
  2644.  
  2645. /* Use this to run interactive tests.  */
  2646.  
  2647. void
  2648. main (argc, argv)
  2649.      int argc;
  2650.      char **argv;
  2651. {
  2652.   char pat[80];
  2653.   struct re_pattern_buffer buf;
  2654.   int i;
  2655.   char c;
  2656.   char fastmap[(1 << BYTEWIDTH)];
  2657.  
  2658.   /* Allow a command argument to specify the style of syntax.  */
  2659.   if (argc > 1)
  2660.     obscure_syntax = atoi (argv[1]);
  2661.  
  2662.   buf.allocated = 40;
  2663.   buf.buffer = (char *) malloc (buf.allocated);
  2664.   buf.fastmap = fastmap;
  2665.   buf.translate = upcase;
  2666.  
  2667.   while (1)
  2668.     {
  2669.       gets (pat);
  2670.  
  2671.       if (*pat)
  2672.     {
  2673.           re_compile_pattern (pat, strlen(pat), &buf);
  2674.  
  2675.       for (i = 0; i < buf.used; i++)
  2676.         printchar (buf.buffer[i]);
  2677.  
  2678.       putchar ('\n');
  2679.  
  2680.       printf ("%d allocated, %d used.\n", buf.allocated, buf.used);
  2681.  
  2682.       re_compile_fastmap (&buf);
  2683.       printf ("Allowed by fastmap: ");
  2684.       for (i = 0; i < (1 << BYTEWIDTH); i++)
  2685.         if (fastmap[i]) printchar (i);
  2686.       putchar ('\n');
  2687.     }
  2688.  
  2689.       gets (pat);    /* Now read the string to match against */
  2690.  
  2691.       i = re_match (&buf, pat, strlen (pat), 0, 0);
  2692.       printf ("Match value %d.\n", i);
  2693.     }
  2694. }
  2695.  
  2696. #endif
  2697.  
  2698.  
  2699. #ifdef NOTDEF
  2700. print_buf (bufp)
  2701.      struct re_pattern_buffer *bufp;
  2702. {
  2703.   int i;
  2704.  
  2705.   printf ("buf is :\n----------------\n");
  2706.   for (i = 0; i < bufp->used; i++)
  2707.     printchar (bufp->buffer[i]);
  2708.   
  2709.   printf ("\n%d allocated, %d used.\n", bufp->allocated, bufp->used);
  2710.   
  2711.   printf ("Allowed by fastmap: ");
  2712.   for (i = 0; i < (1 << BYTEWIDTH); i++)
  2713.     if (bufp->fastmap[i])
  2714.       printchar (i);
  2715.   printf ("\nAllowed by translate: ");
  2716.   if (bufp->translate)
  2717.     for (i = 0; i < (1 << BYTEWIDTH); i++)
  2718.       if (bufp->translate[i])
  2719.     printchar (i);
  2720.   printf ("\nfastmap is%s accurate\n", bufp->fastmap_accurate ? "" : "n't");
  2721.   printf ("can %s be null\n----------", bufp->can_be_null ? "" : "not");
  2722. }
  2723. #endif /* NOTDEF */
  2724.  
  2725. printchar (c)
  2726.      char c;
  2727. {
  2728.   if (c < 040 || c >= 0177)
  2729.     {
  2730.       putchar ('\\');
  2731.       putchar (((c >> 6) & 3) + '0');
  2732.       putchar (((c >> 3) & 7) + '0');
  2733.       putchar ((c & 7) + '0');
  2734.     }
  2735.   else
  2736.     putchar (c);
  2737. }
  2738.  
  2739. error (string)
  2740.      char *string;
  2741. {
  2742.   puts (string);
  2743.   exit (1);
  2744. }
  2745.  
  2746. #endif /* test */
  2747.  
  2748.